C program to reverse a number

In this C program to reverse number article we will make some program which will take integer as  the input from user and print the reverse of that number. If a user gives the integer 123 then the program will print the output as 321. We can do such program in several way. Let’s see some example program of reverse a number using C.

Reverse number program

// reverse number program in C
#include<stdio.h>
int main(){
  int num, rem, revNum = 0;
  printf("Enter an integer to reverse it : ");
  scanf("%d", &num);

  while(num != 0){
    rem = num % 10;
    revNum = (revNum * 10) + rem;
    num /= 10;
  }
  printf("\nReversed number is = %d\n",revNum);
  return 0;
}

Output of this reverse number program:

reverse number program in c, reverse number c program

Reverse number program using recursion in C

// reverse number program using recursion in C
#include <stdio.h>

long revNum(long num){     // function to reverse number
   static long rem = 0;

   if (num == 0){
      return 0;
    }
   rem = rem * 10;
   rem = (num % 10) + rem;
   revNum(num / 10);
   return rem;
}

int main(){
   long number, reminder;
   printf("Enter the number : ");
   scanf("%ld", &number);

   reminder = revNum(number);

   printf("\nReverse number is = %ld\n", reminder);

   return 0;
}

See output of reverse number program:

c reverse number program using recursion or function

Previous pageSwap number

Next pageDecimal to binary