Saturday 18 November 2017

Programs on C - Reverse of a number


#include <stdio.h>
int main()
{
int x; //input number
int rev; //reverse will be stored in this variable
printf("Type a number to get reverse of the number: ");
scanf("%d", &x);
printf("The number is: %d \n", x);
while(x>0)
{
rev = rev*10 + x%10;
x= x/10;
}
printf("Reverse of the number is: %d \n", rev);
return 0;
}
The same can be accomplished by using a function specifically for reversing the number. Also, recursive function can be used to achieve the same.

No comments:

Post a Comment