C Program to Reverse a Number

How to write a C Program to Reverse a Number using While Loop, Functions, and Recursion?.

C Program to Reverse a Number Using While Loop

This program to reverse a number allows the user to enter any positive integer, and then this C program will reverse a number using While Loop

#include <stdio.h>
 
int main()
{
  int Number, Reminder, Reverse = 0;
 
  printf("\nPlease Enter any number to Reverse\n");
  scanf("%d", & Number);
 
  while (Number > 0)
   {
     Reminder = Number %10;
     Reverse = Reverse *10+ Reminder;
     Number = Number /10;
   }
 
  printf("Reverse of entered number is = %d\n", Reverse);
 
  return 0;
}
C Program to Reverse a Number using While Loop

This program for reverse a number in c allows the user to enter any positive integer, and then that number assigned to variable Number. Next, Condition in the While Loop will make sure that the given number is greater than 0

From the above reverse a number in c example, User Entered value: Number= 1456 and Reverse = 0

First Iteration
Reminder = Number %10
Reminder = 1456%10 = 6

Reverse = Reverse *10+ Reminder
Reverse = 0 * 10 + 6 => 0 + 6 => 6

Number = Number /10
Number = 1456 /10 => 145

Second Iteration
From the first Iteration of this C Program to Reverse a Number, the values of both Number and Reverse has changed as Number =145 and Reverse= 6

Reminder= 145 % 10 = 5

Reverse= 6 * 10 + 5 => 60 + 5  => 65

Number= 145 /10 => 14

Third Iteration: From the Second Iteration of this C program, the values of both changed as Number= 14 and Reverse= 65

Reminder= 14%10 = 4

Reverse = 65 * 10 + 4 => 650 + 4 => 654

Number = 14/10 = 1

Fourth Iteration
From the third Iteration, Number= 1 and Reverse= 654

Reminder= 1 %10 = 1

Reverse= 654 * 10 + 1 => 6540 + 1 => 6541

Number= 1/10 => 0

Here, For the next iteration, Number= 0. So, the C Programming while loop condition will fail

C Program to Reverse a Number Using Functions

This reverse a number program allows the user to enter any positive integer. And then, this C program reverse a number using Functions

#include <stdio.h>

int Reverse_Integer (int);

int main()
{
  int Number, Reverse = 0;
 
  printf("\nPlease Enter any number \n");
  scanf("%d", &Number);
 
  Reverse = Reverse_Integer (Number);
  
  printf("Reverse = %d\n", Reverse);
 
  return 0;
}

int Reverse_Integer (int Number)
{
  int Reminder, Reverse = 0;
  
  while (Number > 0)
   {
     Reminder = Number %10;
     Reverse = Reverse *10+ Reminder;
     Number = Number /10;
   }

 return Reverse;
}

C reverse an integer output

Please Enter any value
12345
Reverse = 54321

Within this c program to reverse a number, When the compiler reaches to Reverse_Integer (Number) line in the main() program then the compiler will immediately jump to below function:

int Reverse_Integer (int Number)

We already explained the code LOGIC in the above example. Please refer to C Program to Reverse an Integer Using While Loop Analysis. The last line ends with a return Reverse statement.

C Program to Reverse a Number Using Recursion

This c program to reverse a number allows us to enter any positive integer. Then it will reverse a number using Recursion.

#include <stdio.h>

int Reverse_Integer (int );

int main()
{
  int n, rev = 0;
 
  printf("\nPlease Enter any value\n");
  scanf("%d", &n);
 
  rev = Reverse_Integer (n);
 
  printf("Reverse= %d\n", rev);
 
  return 0;
}

int Reverse_Integer (int n)
{
  static Rem, rev = 0;
  
  if (n > 0)
   {
     Rem = n %10;
     rev = rev *10+ Rem;
     Reverse_Integer (n /10);
   }
  else
    return rev;
}
Please Enter any value
3698
Reverse= 8963

In this program to reverse a number, When the compiler reaches to Reverse_Integer (n) line in the main() program, the compiler will immediately jump to the below function:

int Reverse_Integer (int n)

In this function, Reverse_Integer (n / 10); statement will help to call the function Recursively with the updated value. If you miss this statement, then after completing the first line, it will terminate. For example, Number = 459, then the output will be 9

Let’s see the If condition of this reverse a number in c

if (n > 0) will check whether the number is greater than 0 or not. For Recursive functions, it is crucial to place a condition before using the function recursively. Otherwise, we end up in infinite execution (Same like Infinite Loop).

Comments are closed.