programming-examples/c/Recursion/C Program to find Reverse of a Number using Recursion.c

36 lines
706 B
C
Raw Normal View History

2019-11-15 12:59:38 +01:00
/*
* C program to find the reverse of a number using recursion
*/
#include <stdio.h>
#include <math.h>
int rev(int, int);
int main()
{
int num, result;
int length = 0, temp;
printf("Enter an integer number to reverse: ");
scanf("%d", &num);
temp = num;
while (temp != 0)
{
length++;
temp = temp / 10;
}
result = rev(num, length);
printf("The reverse of %d is %d.\n", num, result);
return 0;
}
int rev(int num, int len)
{
if (len == 1)
{
return num;
}
else
{
return (((num % 10) * pow(10, len - 1)) + rev(num / 10, --len));
}
}