# Thread: Could someone explain this program?

1. ## 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. Did you run it?
What was the result?

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

3. 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. 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. > 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