C Program to reverse the digits of a number

The following is a C program to reverse the digits of a number:

Expected Output:

1st run:

2nd run:

How it works

The following table demonstrates the algorithm we used to reverse the digits of the a given number, assuming n = 1728:

Iteration remainder rev num
After 1st iteration rem = 1728%10 = 8   rev = 0*10+8 = 8 num = 1728/10 = 172
After 2nd iteration rem = 172%10 = 2   rev = 8*10+2 = 82 num = 172/10 = 17
After 3rd iteration rem = 17%10 = 7   rev = 82*10+7 = 827 num = 17/10 = 1
After 4th iteration rem = 1%10 = 1   rev = 827*10+1 = 8271 num = 1/10 = 0

Recommended Reading:

Leave a Comment

%d bloggers like this: