Thread: Can someone help me understand this plz?

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    16

    Can someone help me understand this plz?

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int no,reverse=0,digit;
        
        scanf("%d",&no);
        for( ;no>0; )
        {
            digit=no%10;
            reverse=reverse*10 + digit;
            no=no/10;
        }
        
        printf("%d",reverse);
    
        system("PAUSE");
        return 0;
    }
    I don tunderstand how the code inside the for loop gets you the reverse of the number entered. Please help.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Do you know what the % operator does?
    Do you know what the / operator does?
    How about the * operator?

    12 % 10 is what?
    That, * 10 is what?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    16
    Quote Originally Posted by quzah View Post
    Do you know what the % operator does? yes
    Do you know what the / operator does? yes
    How about the * operator? yes

    12 % 10 is what? 2
    That, * 10 is what? 20

    Quzah.
    still unclear on it.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The mod 10, in a base 10 numbering system like ours, "peels off" the right most digit.

    Then you assign that digit to reverse, and divide the number "no", by 10 to move the next (10's place digit), into the one's place, and you're ready to do it again.

    Since adding is always done from the one's place to the higher places, you've now swapped the number's digits, around.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    16
    Thank you Adak

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quite welcome, AB.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can any1 plz make this assignment
    By jean in forum C Programming
    Replies: 17
    Last Post: 05-13-2009, 09:19 PM
  2. [Request] Need Help Plz
    By TylerD in forum Tech Board
    Replies: 4
    Last Post: 01-03-2009, 09:54 AM
  3. plz help here, noob actually .D
    By BjoRn3n in forum C++ Programming
    Replies: 1
    Last Post: 03-07-2005, 03:04 PM
  4. Anyone plz help me
    By Rose_Flowers in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-17-2003, 12:01 PM
  5. help plz plz
    By nsssn73 in forum C++ Programming
    Replies: 2
    Last Post: 06-03-2002, 08:44 AM