# inverting a number

need a hand with my pascal assignment... pls? i need to write a program that reverses the input number 'n'. i am asked to formulate an equation to come up with the answer. example input/output dialogue:
enter a number: 2546
reversed number: 6452

the program should be simple.. no function or procedure... looping statements only (incrementations and decrementations)... i've tried using for... downto statement but it doesnt show results... please help me.. i'm stuck! your help would be very much appreciated.. thanks...

• : need a hand with my pascal assignment... pls? i need to write a
: program that reverses the input number 'n'. i am asked to formulate
: an equation to come up with the answer. example input/output
: dialogue:
: enter a number: 2546
: reversed number: 6452

Take the starting number and divide it by ten (2546 / 10 = 254 ).
Subtract the resulting number times ten from the original and this will give you the first digit. (2546 - (254 * 10) = 6)
Take the output (initially zero) and multiply it by ten, then add the digit from above.
(0 x 10 = 0 + 6 = 6)
Repeat this again (and again) until the starting number is zero and it would look like so:
[code]
Pass 2) 254 / 10 = 25
254 - (25 * 10) = 4
6 x 10 = 60 + 4 = 64
Pass 3) 25 / 10 = 2
25 - (2 * 10) = 5
64 x 10 = 640 + 5 = 645
Pass 4) 2 / 10 = 0
2 - (0 * 10) = 2
645 x 10 = 6450 + 2 = 6452
[/code]

There are many ways, but hopefully this is basic enough to follow and uses only basic mulitplication & division. It could also be done faster and easier with MOD operations or converting it to a string, reversing the string and displaying the output.

Phat Nat
• : need a hand with my pascal assignment... pls? i need to write a
: program that reverses the input number 'n'. i am asked to formulate
: an equation to come up with the answer. example input/output
: dialogue:
: enter a number: 2546
: reversed number: 6452
:
: the program should be simple.. no function or procedure... looping
: statements only (incrementations and decrementations)... i've tried
: using for... downto statement but it doesnt show results... please
: help me.. i'm stuck! your help would be very much appreciated..
: thanks...
:
My approach would be to read in the number, convert it to a string, reverse the string and then write out the number.
[code]
begin
ReadLn (n) ; { program will crash here if the operator does
not enter a valid number }
Str (n:0, sin) ;
sout := '' ;
for i := length(sin) downto 1 do
sout := sout + sin[i] ;
Val (s, n, code) ;
WriteLn (n)
end.
[/code]
I may have the order of the parameters wrong for Str and Val, and hopefully the use of Str and Val does not violate the requirement ".. no function or procedure..." since you do not have to write them. A simpler approach would be to input and output the number as a string and not have to use Str and Val, but then any string could be reversed, not just a number. I'm assuming the assignment requires the input to be a number (integer or real).