Q:

Write C program to find reverse of a number using recursion

0

Write C program to find reverse of a number using recursion

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

I have used Code::blocks 12 compiler for debugging purpose. But you can use any C programming language compiler as per your availability.

#include <stdio.h>
#include <math.h>
 
 
//Fuction declaration
int reverseNumber(int num);
 
 
int main()
{
    int num, reverse;
 
    // Inputting number from user
    printf("Enter any number: ");
    scanf("%d", &num);
 
    // Calling function to reverse any number
    reverse = reverseNumber(num);
 
    printf("Reverse of %d : %d", num, reverse);
 
    return 0;
}
 
 
 
//Recursive function to find reverse of any number
 
int reverseNumber(int num)
{
    // Find total digits in num
    int digit = (int) log10(num);
 
    // Base condition
    if(num == 0)
        return 0;
 
    return ((num%10 * pow(10, digit)) + reverseNumber(num/10));
}

Result:

Enter any number: 123

Reverse of 123 : 321

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write C program to find LCM of two numbers using r... >>
<< Write C program to print even or odd numbers in gi...