#include <stdio.h> main() { unsigned int a,b=0; printf("Number:"); scanf("%d",&a); while (a!=0) { b=b*10+a%10; a=a/10; } printf("Reversed number:%u\n",b); getch(); return 0; } 

this code displays the number in reverse order,

b = b * 10 + a% 10; a = a / 10; explain pzhl what occurs in these two lines.

The program works all the rules, but I do not quite understand what kind of calculations occur with b if it is zero.

  • one
    scanf("%d",&a,b); - this is not a mistake, but it is nonsense. Also: %d format for unsigned int ? For unsigned int , the format is %u . And int main(void) , not main() . - AnT
  • Well, look. a%10 is the last digit of a . Next, a/10 is the number of a without the last digit. Since a=a/10; removes the last digit from a , while (a!=0) means "so far there are unprocessed digits in a ". - HolyBlackCat
  • HolyBlackCat, what did you explain, I understand, but does b = b * 10? - user310127

1 answer 1

 while (a!=0) { b=b*10+a%10; a=a/10; } 

Let's take an example ... a = 234

  ab 234 0 b=b*10+a%10; 234 0*10+4 = 4 a=a/10; 23 4 b=b*10+a%10; 23 4*10+3 = 43 a=a/10; 2 43 b=b*10+a%10; 2 43*10+2 = 432 a=a/10; 0 432 

All, a==0 , the loop is complete.

b=b*10+a%10; - essentially a shift of b left by one digit and the addition of the last digit of a .

So you understand?

  • I forgot that the cycle repeats for now and not equal to 0, Thanks for explaining)) - user310127