Program to reverse any number using recursion


Default
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//Write a program to find reverse of a given number using recursion
//Eg: 29943 reverse is 34992
//Author: admin@aimforjob.com
 
#include <stdio.h>
int revnum(int );
int sum=0;
int main()
{
    int inp;
    printf("Enter a number:");
    scanf("%d",&inp);
    if(inp<0)
        printf("Invalid input...\n");
    else if(inp<10)
        printf("Reversed Number=%d\n",inp);
    else
        printf("Reversed Number=%d\n",revnum(inp));
    return 0;
}
int revnum(int n)
{
    if(n==0)
        return sum;
    else{
        sum=(sum*10)+(n%10);
        revnum(n/10);
    }
}

No comments:

Post a Comment