By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,503 Members | 1,278 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,503 IT Pros & Developers. It's quick & easy.

Loop to print reverse of any size number

P: 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
Share this Question
Share on Google+
6 Replies


ruskalym
P: 65
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

P: 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
Expert 2.5K+
P: 3,652
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
Expert 2.5K+
P: 3,652
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
Expert 2.5K+
P: 3,652
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

ruskalym
P: 65
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.