C Program to Reverse a Number using While Loop

C program to reverse a number: First we accept a number. We get the last digit of a number using modulus operator (%) and use it to construct the reverse number. This program is implemented using do while loop but you can also use while loop or for loop.

C program to reverse a number

/* Aim: Write a C program to accept an integer and reverse the number */

#include<stdio.h>

int main()
{ 
	int r,num,rev=0,original;
 
	printf("\n Enter any number:- ");
	scanf("%d",&num);

	original=num;

	do
	{ 
		r=num%10;
		rev=rev*10+r;
		num/=10;
	}while(num>0);

	printf("\n %d is reverse number of %d \n \n",rev,original);

	return 0;
}

/* Output of above code:-

 Enter any number:- 1234

 4321 is reverse number of 1234 

*/