For example, if the user enters 5143, the function would reverse it so that it becomes 3415.
#include<stdio.h>
void
main ()
{
int num=0;
clrscr();
printf(" Enter a number to
reverse : ");
scanf("%d",&num);
printf(" Reverse of %d is
%d\n",num,reverse(num));
getch();
}
int
reverse(int num)
{
int res=0,temp=num,k,a=0,i;
/* finds the number or digits*/
for(;temp/10!=0;temp/=10)
a++;
for(;num/10!=0;)
{
k=num%10;
for(i=0;i<a;i++)
{
k*=10;
}
num/=10;
a--;
res+=k;
if (num<10)
res+=num;
}
return res;
}
No comments:
Post a Comment