C program to find reverse of a number

Here we are going to reverse a number in c, for this we can use two ways to find reverse of a number so here we go :-

  • Reverse a number without using recursion
  • Reverse a number using recursion

So Let’s Not waste the time and Lets first see our prerequisite then see our code

prerequisite :-

To understand how to find reverse of a number, you should have the knowledge of following C programming topics:

  • C Programming Operators
  • C Programming while and do…while Loop

C program to find reverse of a number

#include <stdio.h>
void main()
{
   int n, reverse = 0;
    printf("Enter a number to reverse\n");
   scanf("%d", &n);
    while (n != 0)
   {
      reverse = reverse * 10;
      reverse = reverse + n%10;
      n = n/10;
   }
    printf("Reverse of entered number is = %d\n", reverse);
    getch();
}

click here to download code

This program takes an integer input from the user. Then the while loop is used until n != 0  is false.

In each iteration of while loop, the remainder when n is divided by 10 is calculated and the value of n is reduced by times.

Output

Enter an integer: 123
Reversed Number = 321

 

Reverse number C program using recursion

#include <stdio.h>
 void main()
{
   int n, reverse = 0;
   printf("Enter a number to reverse\n");
   scanf("%d", &n);
    while (n != 0)
   {
      reverse = reverse * 10;
      reverse = reverse + n%10;
      n = n/10;
   }
    printf("Reverse of entered number is = %d\n", reverse);
    getch();
}

click here to download code

Output

Enter an integer: 123
Reversed Number = 321

Leave a Reply

avatar
  Subscribe  
Notify of