Pages

Tuesday, 23 June 2015

Reverse a number

C program to reverse a number

Coding:

#include<stdio.h>
int main()
{
int n,rn=0,r,no,count=0,flag=0;
printf("Please enter the number:");
scanf("%d",&n);
no=n;
while(n>0 && n%10==0)
{
r=n%10;
n=n/10;
if(r==0)
count++;
}
printf("After reversal:");
while(count>0)
{
printf("0");
count--;
}
n=no;
while(n>0)
{
r=n%10;
n=n/10;
rn=rn*10+r;
}
printf("%d",rn);
return 0;
}


Output:
Please enter the number:156
After reversal:651