1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

C program to reverse a number.

Discussion in 'Microcontrollers' started by Parth86, Aug 24, 2017.

  1. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    515
    Likes:
    5
    Hi
    I am able to write program that print reverse number example 10 9 8 7 6 5 4 3 2 1

    Code (c):

    #include <stdio.h>

    int main()
    {
        int i =10;              / decleration of variable

        while(i>=1)
        {
            printf("%d \n", i);
            i--;
        }

        return 0;
    }
     
    Now I want to write c program that reverse the number entered by the user and then prints the reversed number on the screen. For example if user enter 123 as input then 321 is printed as output. I have seen some program on internet but I don't understand the logic behind program. so looking help with basics
     
  2. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,022
    Likes:
    317
    Location:
    Brisbane Australia
    ONLINE
    Something like,
    Code (text):

        unsigned char i,str="12345";
        for(i=0;i<5;i++){
            printf("%d \n", str[4-i]);
        }
    You can do a for that decrements but it's a little messy with unsigned numbers.

    Mike.
     
  3. ci139

    ci139 Active Member

    Joined:
    Apr 12, 2012
    Messages:
    404
    Likes:
    34
    Code (text):

    #include  <stdio.h>

    int main(void)
    { __int64 x=76543210;
      __int64 y=0;
      __int64 g=0;
      while(x>0)
      { g=10*g+9;
        y=10*y+x%10;
        x=x/10;
      }
      printf("x = %d\n", x);
      printf("y = %d\n", y);
      printf("g = %d\n", g);
      return 0;
    }

    x = 0
    y = 1234567
    g = 99999999
     
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Cicero

    Cicero Active Member

    Joined:
    Nov 21, 2014
    Messages:
    335
    Likes:
    36
    Location:
    UK

    Here's a quick reverse integer function with comments.

    Code (c):

    int reverse(int x) {
        int temp, digit, res = 0;
     
        while (x != 0) {
            // mod 10 gets digit
            digit = x % 10;
         
            // shift previous result over and add the newest digit - store temp for overflow comparison
            temp = res * 10 + digit;
         
            // prevent int overflow by checking if reverse operation is the same
            if ((temp - digit) / 10 != res)
                return 0;
             
            // not overflow, so store new result for next interation
            res = temp;
         
            // remove digit from original number, shift down
            x /= 10;
        }
     
        return res;
    }

     
     
    Last edited: Aug 24, 2017
  6. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    515
    Likes:
    5
    I said that I have seen many programs on internet and I am just asking for basic understanding. suppose how do you do manually than using by program. so I think I have to understand what's happening and than I want to go with program.
     
  7. Cicero

    Cicero Active Member

    Joined:
    Nov 21, 2014
    Messages:
    335
    Likes:
    36
    Location:
    UK
    That's why I added the code comments, for understanding. Its just maths. It makes me think you don't understand what a modulo operation is?

    The method is to do one digit at a time from the integer until nothing is left.
    From the input number, get the smallest to biggest digit using modulo 10, and divide by 10 to shift the integer down.
    For the reversed result, you do the opposite. Use the digits acquired above but add them to the result after multiplying by 10 for each one.

    Example 345, need to get the 5, use module 10. Then need the 4, so take 345/10 = 34, and module 10 = 4. Need the 3, so take 34/10 mod 10 = 3.
    For the result you just do the opposite with those numbers. 5, then when you have the 4, take 5*10+4=54, when you have the 3, 54*10+3.

    Example: Start x = 345, res=0
    First loop:
    • digit = x % 10 = 5
    • temp = res * 10 + digit = 0*10 + 5 = 5
    • if ((temp - digit) / 10 != res) = if ((5-5)/10 != 0) = if (0!=0)
    • res = temp = 5
    • x/=10 = x/10 = 345/10 = 34

    Second loop:
    • digit = x % 10 = 34%10 = 4
    • temp = res * 10 + digit = 5*10 + 4 = 54
    • if ((temp - digit) / 10 != res) = if ((54-4)/10 != 5) = if (5!=5)
    • res = temp = 54
    • x/=10 = x/10 = 34/10 = 3

    Third loop:
    • digit = x % 10 = 3%10 = 3
    • temp = res * 10 + digit = 54*10 + 3 = 543
    • if ((temp - digit) / 10 != res) = if ((543-3)/10 != 54) = if (54!=54)
    • res = temp = 543
    • x/=10 = x/10 = 3/10 = 0

    No forth loop because x=0
    return result = res = 543
     
    • Like Like x 1
  8. ci139

    ci139 Active Member

    Joined:
    Apr 12, 2012
    Messages:
    404
    Likes:
    34
    Last edited: Aug 25, 2017

Share This Page