Wednesday, December 25, 2013

Write a C program to reverse a number

Description-This program reverse the number entered by the user and then prints the reversed number on the screen.
For example if user enter 123 as input then 321 is printed as output. In our program we use modulus (%) operator to obtain the digits of a number. To invert number look at it and write it from opposite direction or the output of code is a number obtained by writing original number from right to left. To reverse or invert large numbers use long data.

Source Code:
#include <stdio.h>
int main()
{
int n,rem, reverse = 0;
printf("Enter a number to reverse\n");
scanf("%d",&n);
while (n != 0)
{
rem=n%10;
reverse = reverse *10+ rem;
n = n/10;
}
printf("Reverse of entered number is = %d\n", reverse);
return 0;

}

No comments:

Post a Comment