using mod
Author |
Message |
foo fighter
|
Posted: Thu Nov 24, 2005 5:44 pm Post subject: using mod |
|
|
Ok here's what i want my program to do:
I want the user to input a number. Then its divided by 2 continuosly until its at zero and for each time it divides if theres a demainder i want it to output a 1 and if no remaider then output a 0.
my friend said i should use mod, but i cant figure it out |
|
|
|
|
|
Sponsor Sponsor
|
|
|
MysticVegeta
|
Posted: Thu Nov 24, 2005 5:50 pm Post subject: (No subject) |
|
|
remainder is the mod function.
for example:
put 4 mod 2.
Output will be 0
Because 4/2 = 2. Remainder = 0 |
|
|
|
|
|
foo fighter
|
Posted: Thu Nov 24, 2005 6:24 pm Post subject: (No subject) |
|
|
ummm... okay, but its not rounding numbers with remainders of 1, it just puts them as 0.5 |
|
|
|
|
|
person
|
Posted: Thu Nov 24, 2005 6:56 pm Post subject: (No subject) |
|
|
x:= x div 2
y:= x mod 2
put x,y[/quote] |
|
|
|
|
|
[Gandalf]
|
Posted: Fri Nov 25, 2005 4:38 pm Post subject: (No subject) |
|
|
Yes, as person's example shows, you must use the div command for the mod (remainder) to be accurate. |
|
|
|
|
|
Dan
|
Posted: Fri Nov 25, 2005 5:16 pm Post subject: (No subject) |
|
|
it should be noted that such a progame whould never aucatly hit 0 unless sigficant rounding was used..... |
Computer Science Canada
Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more! |
|
|
|
|
person
|
Posted: Fri Nov 25, 2005 6:50 pm Post subject: (No subject) |
|
|
put 1 div 2
outputs
0 |
|
|
|
|
|
Dan
|
Posted: Fri Nov 25, 2005 9:58 pm Post subject: (No subject) |
|
|
person wrote: put 1 div 2
outputs
0
Thats b/c the div comand is not the same as dividing 2 numbers. It dvides them and then cuts of the decmans (ether by round or truncating them, i whold have to check).
Since he side divides and not divides and truncates........
Tho i blive that at some point turing whould start round even the number divide using the noraml divsion comand. But a exit condtion based soly on that whould not be good progaming. |
Computer Science Canada
Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more! |
|
|
|
|
Sponsor Sponsor
|
|
|
|
|