Loop to print reverse of any size number

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bitong
    New Member
    • Sep 2006
    • 40

    Loop to print reverse of any size number

    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
  • ruskalym
    New Member
    • Sep 2007
    • 65

    #2
    Code:
    Code has been removed.  Please see our Posting Guidelines.
    
    MODERATOR
    In recursion we trust!

    Just call revhat() with your number.
    Last edited by Ganon11; Sep 27 '07, 03:24 PM. Reason: Removed spoonfeeding code

    Comment

    • bitong
      New Member
      • Sep 2006
      • 40

      #3
      What's revhat()? could you please elaborate more?

      Originally posted by ruskalym
      Code:
      Code has been removed.  Please see our Posting Guidelines.
      
      MODERATOR
      In recursion we trust!

      Just call revhat() with your number.

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        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...

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          This simple code compiled:

          [CODE=cpp]#include <iostream>
          using namespace std;

          int main() {
          cout << "Please enter a number: ";
          int y;
          cin >> y;
          cout << endl;

          int x, z;

          for (x = y; x > 0; x /= 10){
          z = x % 10;
          cout << z;
          }
          cout << endl;
          return 0;
          }
          [/CODE]

          and produced the following result:

          Code:
          $ digit.exe
          Please enter a number: 1231231231
          
          1321321321
          Anything more than 10 digits was not working - possibly because you are exceeding the maximum amount for an int?

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            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.

            Comment

            • ruskalym
              New Member
              • Sep 2007
              • 65

              #7
              Originally posted by bitong
              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.

              Comment

              Working...