C Program to Reverse a Number: To reverse each digit in a number programmatically. Example: If the number is 431 then the reverse number will be 124.
We use the modulus(%) operator in the program to obtain the digits of a number.

Formula to reverse number
x=n%10;
rev=rev*10+x;
n=n/10;

Algorithm to reverse a Number

  1. Take input number
  2. while number > 0
    x= number %10;
    rev=rev*10+x;
    number = number /10;
  3. Print reverse number

C Program to Reverse a Number

#include<stdio.h>

void main()

{

int temporary ,n,rev=0,x;

printf("Enter a number\n");

scanf("%d",&n);

temporary =n;

while(n>0)

{

x=n%10;

rev=rev*10+x;

n=n/10;

}

printf("The reverse of %d is %d\n",temporary ,rev);

}

Output:

C Program to Reverse a Number

Write A Comment