: 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.
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.
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).