User Define Function- Reverse Number

Q. Write a C program to create a function that would reverse the any number.
For example:
Assume enter number = 57429
Result = 92475

Ans.

/*c program for reverse number using user define function- rev */
#include<stdio.h>
int rev(int );
int main()
{
 int num,res;  // res = result
 printf("Enter any number : ");
 scanf("%d", &num);
 res = rev(num);
 printf("Reverse order number = %d",res);
 return 0;
}

int rev(int n)
{
 int r=0;
 for(; n>=1; n=n/10)
 {
   r = r*10 + n%10;
   return r;
 }
}

The output of above program would be:


Output of reverse number using user define function C program
Figure: Screen shot for reverse digit using
user define function C program


Comments