hi friendz,
how can i display the given number in reverse order,whatever may b the data type without using character array?
Printable View
hi friendz,
how can i display the given number in reverse order,whatever may b the data type without using character array?
try to use instring and substring ,length or the corresponding C/ C++ function in a simple for loop.
can u plz explain me with example..
first find out the length of the string.
then run the for loop decrement by -1.
inside the loop findout the last character of the string and store to another variable .Continue the process till you have read all the characters of the string and concatinate to the variable which is storing the last character.
let me give you the pseudo code
[B]actual_number number;
rev_number number;
rev_number =0;
while actual_number >0 loop
rev_number = rev_number * 10 + (actual_number mod 10);
actual_number = actual_number /10;
end loop;
[/B]
How the above code works,
let us assume that the actual number is 124
and rev_number = 0 before it starts executing the loop,
while 124 >0 true =>
rev_number = rev_number * 10 + (actual_number mod 10)
= 0 *10 + 4 =4
actual_number = 124/10 =12
while 12 >0 true =>
rev_number = rev_number * 10 + (actual_number mod 10)
= 4 *10 + 2 =42
actual_number = 12/10 =1
while 1 >0 true =>
rev_number = rev_number * 10 + (actual_number mod 10)
= 42 *10 + 1 =421
actual_number = 1/10 =0
while 1 >0 false => come out of the loop
Note that I have give you only the logic not the code as i don't remember the syntax of C.
Hope this helps you...
The code logic will work only for number datatypes ,but if you use the corresponding c/c++ functions of length,substring,instring you can also reverse strings.
ok.. thanks to all...