Sunday 20 May 2012

Write a program to reverse the digits of a number using for loop.


//program to reverse the digits of a number using for loop
void main()
{
int num,rev=0,a,b,i;
clrscr();
printf("Enter any number ");
scanf("%d",&num);
for(a=num;a>0;a/=10)
{
b=a%10;
rev=rev*10+b;
}
printf("\n%d is the reverse of %d",rev,num);
getch();
}
Output
Enter any number 1234
4321 is the reverse of 1234

No comments:

Post a Comment