Thread: Could someone explain this program?

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    16

    Could someone explain this program?

    It's in my book, but unexplained. It reverses a 2-digit number. But I don't get how the operations work....Shouldn't it give zero each time? Could someone please explain what's happening?

    Code:
    #include<stdio.h>
    
    int main(void)
    {
       int number, last_digit, next_digit, rev_number;
    
      printf("Enter the number to be reversed:");
      scanf("%d", &number);
    
      last_digit=number-((number/10)*10);
    
      rev_number=last_digit;
    
      next_digit=(number/10) - ((number/100)*10);
    
      rev_number=(rev_number*10)+next_digit;
    
      printf("The reversed number is:%d", rev_number);
      getch();
      clrscr();
    
    }

  2. #2
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Did you run it?
    What was the result?

    I also have a feeling, that, this is incorrect, and way too complex also.

  3. #3
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Assume number =532

    lastdigit=532-((532/10)*10=532-53*10=2

    Shouldn't it give zero each time?
    Note that integer/integer is always rounded off to integer (in programming). So 532/10=53.2=53

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    16
    Note that integer/integer is always rounded off to integer (in programming). So 532/10=53.2=53
    Oh, all right, then I get it... Thanks!

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > rounded off to integer
    Truncated to be precise

    6.6 rounded to an integer is actually 7 (as 7 is the closest integer).

    However, 6.6 truncated is 6. There is a big difference.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  4. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM