C program to find the reverse of a number using recursion

c program to find the reverse of a number using recursion

C program to find reverse of entered number using function recursion. 

Recommended: C Program to find GCD of number using Recursive Function

Program:

/* C program to find the reverse of a number using recursion by Slashmycode.com
 */
#include <stdio.h>
#include <math.h>

int rev(int, int);

void 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;
}
/* C program to find the reverse of a number using recursion by Slashmycode.com
 */
int rev(int num, int len)
{
    if (len == 1)
    {
        return num;
    }
    else
    {
        return (((num % 10) * pow(10, len - 1)) + rev(num / 10, --len));
    }

}



Output:

output of C program to find the reverse of a number using recursion






No comments:

Post a Comment

Feel free to ask us any question regarding this post