Thread: Unable to get Reverse of 5-digit number as output

  1. #1
    Registered User Anish Kuriakose's Avatar
    Join Date
    Jan 2014
    Location
    Madel, Tivim, Bardez, North Goa, Goa, India
    Posts
    7

    Question Unable to get Reverse of 5-digit number as output

    Hi,


    This is the first time that I'm posting on this forum. I just singed up today. I had done a course on Computer Application Development about 7-8 years ago. I just wanted to brush up on my coding skills. Thus, I started off with C.


    As part of getting myself oriented I am doing the basic coding exercises. At this stage I am not supposed to use 'if-else' decision statements or loops.


    The Exercise is to "Write a programme to reverse a 5-digit number input by the user." I have typed the following code in Turbo C. However, I'm not getting the expected result...


    I know some people might say that Turbo C is dinosaur like. However, I'd like to know why I'm not getting the correct result, even though there are no errors found during compiling. Anyway, if I'm left with no choice, then, I might as well used a better IDE or something... Following is my code. Please help...

    Code:
    /* Program to reverse a 5-digit number */
    
    
    #include<stdio.h>
    #include<conio.h>
    
    
    int main ()
    {
      int num, n1,n2,n3,n4,n5,rev;
      
      clrscr();
      printf("\nPlease enter a 5-digit number: ");
      scanf("%d",&num);
      
      n1 = num % 10;
      num = num / 10;
      
      n2 = num % 10;
      num = num / 10;
    
    
      n3 = num % 10;
      num = num / 10;
    
    
      n4 = num % 10;
      num = num / 10;
    
    
      n5  = num;
    
    
      rev = n1*10000 + n2 * 1000 + n3 * 100 + n4 * 10 + n5;
    
    
      printf("\nReverse of the number is %d",rev);
      getch();
      
      return 0;
    }

    I wonder what exactly is the problem... I wonder what it is that is invisible to my eyes....


    Thanks in advance.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If it doesn't reverse a number, what does it do? (That is to say, what behavior of the program makes you believe there is an error?)

  3. #3
    Registered User Anish Kuriakose's Avatar
    Join Date
    Jan 2014
    Location
    Madel, Tivim, Bardez, North Goa, Goa, India
    Posts
    7

    Unhappy

    It prints: "Reverse of the number is -11215"
    I fail to understand why the values of n1, n2, n3, n4 and n5 do not remain the same.

    I tried inserting printfs after each modulus operation, i.e.

    (Let's assume that the 5-digit number input by user is 12345)

    n1 = num %10;

    printf("Value of n1 = %d",n1);

    It prints: "Value of n1 is 5"

    Now, this is what I want n1 to store. However, the value of n1 somehow seems to get modified at the final arithmetic operation stage, I suppose, i.e.

    rev = n1 * 10000

    at this above step....
    Last edited by Anish Kuriakose; 01-13-2014 at 10:26 PM.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Anish Kuriakose View Post
    It prints: "Reverse of the number is -11215"
    What do you mean? It prints that no matter what you enter? What input did you give?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User Anish Kuriakose's Avatar
    Join Date
    Jan 2014
    Location
    Madel, Tivim, Bardez, North Goa, Goa, India
    Posts
    7
    I'm sorry for creating a confusion. I assumed the input as 12345. Well, the problem is that no matter what input I give (I mean a valid 5 digit number) it's giving me a negative value... (e.g. -11215 or -11561 or something of that sort.) So, that simply means my code is wrong..., right?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,177
    The logic looks correct, and I am unable to replicate your problem when I tested with:
    Code:
    #include<stdio.h>
    
     
    int main(void)
    {
        int num, n1, n2, n3, n4, n5, rev;
    
        printf("\nPlease enter a 5-digit number: ");
        if (scanf("%d", &num) == 1)
        {
            n1 = num % 10;
            num = num / 10;
    
            n2 = num % 10;
            num = num / 10;
    
            n3 = num % 10;
            num = num / 10;
    
            n4 = num % 10;
            num = num / 10;
    
            n5  = num;
    
            rev = n1 * 10000 + n2 * 1000 + n3 * 100 + n4 * 10 + n5;
    
            printf("\nReverse of the number is %d\n", rev);
        }
    
        return 0;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,457
    Since you're getting a negative number this suggests to me that Turbo C is using 16-bit ints and you're getting a signed overflow; i.e. the range of a signed 16-bit integer is −32,768 to 32,767 (assuming 2s complement representation). Therefore if you enter any number > 32767 things are not likely to be what you expect.

    Try this on your compiler:

    Code:
    #include<stdio.h>
     
    int main ()
    {
      int num, n1,n2,n3,n4,n5,rev;
    
      num =65535;
    
      printf("Number is %d\n",num);
       
      n1 = num % 10;
      num = num / 10;
       
      n2 = num % 10;
      num = num / 10;
     
     
      n3 = num % 10;
      num = num / 10;
     
     
      n4 = num % 10;
      num = num / 10;
     
     
      n5  = num;
     
     
      rev = n1*10000 + n2 * 1000 + n3 * 100 + n4 * 10 + n5;
     
     
      printf("\nReverse of the number is %d",rev);
       
      return 0;
    }
    If Turbo C is indeed using 16-bit ints then you'll get something along the lines of

    Code:
    Number is -1
    
    Reverse of the number is -10000
    I have no idea if Turbo C has limits.h but if it does (it should) check to see what the values of INT_MIN and INT_MAX are.

    For example:
    Code:
    #include <stdio.h>
    #include <limits.h>
     
    int main(void)
    {
        printf("Range is %d to %d\n", INT_MIN, INT_MAX);
        return 0;
    }

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    For some reason, your compiler is using a 16-bit integer. 54321 is too big to properly fit and wraps around to a negative number. Look up "two's complement representation" for more info.

    So, unless you're using a very old computer, you really do have to update that compiler!
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  9. #9
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,457
    Quote Originally Posted by laserlight View Post
    The logic looks correct, and I am unable to replicate your problem when I tested with:
    It can be replicated using short rather than int

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    In other words it does come down to TurboC being a dinosaur in this case.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Registered User Anish Kuriakose's Avatar
    Join Date
    Jan 2014
    Location
    Madel, Tivim, Bardez, North Goa, Goa, India
    Posts
    7
    Thank you so much Hodor. You and all the other users have pointed me to the right direction. You're right. I'm using the 16-bit compiler and that's the root cause of the problem. BTW, Turbo C does have limits.h and it does display the limits. Thank you all. I feel so glad that I'm now part of this great forum. So much of intellect. I should have joined long ago.

  12. #12
    Registered User Anish Kuriakose's Avatar
    Join Date
    Jan 2014
    Location
    Madel, Tivim, Bardez, North Goa, Goa, India
    Posts
    7
    Lol

  13. #13
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Then replace 'int' declarations with 'long' to force it to use 32-bit integers. Use %ld in print format specifier.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reverse a five digit number in C
    By Aamir Masood in forum C Programming
    Replies: 13
    Last Post: 11-08-2010, 11:37 AM
  2. Replies: 2
    Last Post: 10-31-2009, 06:49 PM
  3. Adding a Large number digit by digit
    By mejv3 in forum C Programming
    Replies: 23
    Last Post: 09-21-2007, 03:00 PM
  4. Adding a Large number digit by digit
    By mejv3 in forum C Programming
    Replies: 1
    Last Post: 09-14-2007, 03:28 AM
  5. outputs number digit by digit
    By Unregistered in forum C++ Programming
    Replies: 14
    Last Post: 05-17-2002, 04:43 PM