-
Junior Member
reverse number
hi friendz,
how can i display the given number in reverse order,whatever may b the data type without using character array?
-
Re: reverse number
try to use instring and substring ,length or the corresponding C/ C++ function in a simple for loop.
-
Junior Member
Re: reverse number
can u plz explain me with example..
-
Re: reverse number
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.
-
Expert Member
Re: reverse number
let me give you the pseudo code
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;
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...
-
Re: reverse number
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.
-
Junior Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules