Tuesday, October 27, 2015

Reverse Number Using Recursion in C - Working Code

#include<stdio.h>
#include<conio.h>
int reverse(int);
void main()
{
    int n,r;
    clrscr();
    printf("Enter a number");
    scanf("%d", &n);
    r = reverse(n);
    printf("%d\n",r);
    getch();
}

int reverse(int n)
{
    static long r = 0;
    if(n == 0)
    {
       return 0;
    }
    r = r * 10;
    r = r + n % 10;
    reverse(n/10);
    return r;
}

To Compile Press Alt+f9 
To Run Press Ctrl+f9

Output:
     

No comments:

Post a Comment