469,600 Members | 2,453 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,600 developers. It's quick & easy.

Loop to print reverse of any size number

40
The problem is input a number then the output should be in reverse order..
I already made a code but only good for 9 - digit only...What I want to do is to hold infinite digit, in other words don't have a limit on how many digit that I want to input..

for (x = y;x>0;x=x/10){
z = x % 10;
cout<<z;
}

* code good for 9 - digit only, if I input 10 digit its output is not correct anymore..I used int as data type
Pls. Help
Sep 27 '07 #1
6 6626
Expand|Select|Wrap|Line Numbers
  1. Code has been removed.  Please see our Posting Guidelines.
  2.  
  3. MODERATOR
In recursion we trust!

Just call revhat() with your number.
Sep 27 '07 #2
bitong
40
What's revhat()? could you please elaborate more?

Expand|Select|Wrap|Line Numbers
  1. Code has been removed.  Please see our Posting Guidelines.
  2.  
  3. MODERATOR
In recursion we trust!

Just call revhat() with your number.
Sep 28 '07 #3
Ganon11
3,652 Expert 2GB
That post had some full code, which is not allowed (see our Posting Guidelines).

I'm having trouble understanding why this code doesn't work universally...
Sep 28 '07 #4
Ganon11
3,652 Expert 2GB
This simple code compiled:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5.     cout << "Please enter a number: ";
  6.     int y;
  7.     cin >> y;
  8.     cout << endl;
  9.  
  10.     int x, z;
  11.  
  12.     for (x = y; x > 0; x /= 10){
  13.         z = x % 10;
  14.         cout << z;
  15.     }
  16.     cout << endl;
  17.     return 0;
  18. }
  19.  
and produced the following result:

Expand|Select|Wrap|Line Numbers
  1. $ digit.exe
  2. Please enter a number: 1231231231
  3.  
  4. 1321321321
Anything more than 10 digits was not working - possibly because you are exceeding the maximum amount for an int?
Sep 28 '07 #5
Ganon11
3,652 Expert 2GB
EDIT: After a few more test runs, I'm sure of it. The maximum value for an int is something like 2,148,000,000, which is 10 digits. If you input anything greater than that, your program won't work (because an int isn't big enough to hold that). This is an expected error, so I wouldn't worry about it. If you need to use numbers bigger than 10 digits, try a long int.
Sep 28 '07 #6
What's revhat()? could you please elaborate more?
I am sorry but code was deleted by moderator.

I provided code implementing following recursive algorithm :

revrec() function takes an integer argument :
if it is not 0 then last digit is printed and revrec() is called again with argument divided by 10.

The revhat() function is intended to check if initial argument is 0, in which case it is printed. Else, revrec() is called with argument.
Sep 28 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Rajorshi Biswas | last post: by
reply views Thread by Vedran Vukotic | last post: by
10 posts views Thread by aatish19 | last post: by
15 posts views Thread by Alex Snast | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.