C Program That Uses The Recursive Function To Reverse A Given Number

This tutorial will teach you how to write a C program that will reverse a given number. We’ll look at two methods for reversing a number:
1) Making Use of Recursion
2) Employing a While loop

Example 1: Use recursion to reverse a given number.

We created a user defined function reverse function in this program, which takes a number as a parameter and returns the inverse of that number.
This function’s logic is as follows: In this function, we divide the input number by 10 and store the remainder in a variable sum; the remainder is the number’s last digit. This function then calls itself for the number/10, which ensures that the number without the last digit is passed back to the same function.
In the following iteration, the second last digit becomes the second digit of sum (because sum=sum*10+rem). This process is repeated until all digits from the original number are removed in reverse order and appended to the variable sum. This sum variables contains the inverse of the original number at the end of the recursion.

#include <stdio.h>
int sum=0,rem;
int reverse_function(int num){
    return sum;
  return sum;
int main(){
  int num,reverse_number;

  //Take the number as an input from user
  printf("Enter any number:");

  //Calling user defined function to perform reverse
  printf("The reverse of entered number is :%d",reverse_number);
  return 0;

Example 2: Use a While loop to reverse a number.

In this program, we use a while loop to reverse the input number.

#include <stdio.h>
int main()
  int num,rem,reverseNum=0;
  //Input number
  printf("Enter an integer number: ");

    rem = num%10;
    reverseNum = reverseNum*10 + rem;
    num = num/10;

  printf("Reverse of the entered number is: %d", reverseNum);
  return 0;


Enter an integer number: 8904
Reverse of the entered number is: 4098

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button