Saturday 31 August 2013

C program to reverse a number using recursion



C program to reverse a number using recursion

#include<stdio.h>
int main(){
    int num,reverse;

    printf("Enter any number: ");
    scanf("%d",&num);

    reverse=rev(num);
    printf("Reverse of number: %d",reverse);
    return 0;
}
int rev(int num){
    static sum,r;

    if(num){
         r=num%10;
         sum=sum*10+r;
         rev(num/10);
    }
    else
         return 0;
    return sum;
}

Sample output:
Enter any number: 456
Reverse of number: 654

0 comments:

Post a Comment

 

C Programming Language Interview Questions and Answers Tutorial for beginners. Copyright 2013 All Rights Reserved