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

It allows the user to enter any positive integer, and then this 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 allows the user to enter any positive integer, and then that number is assigned to a variable. Next, Condition in the While Loop will make sure that the given number is greater than 0

From the above 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 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 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 program allows the user to enter any positive integer. And then, this program reverse a number using Functions

#include <stdio.h>

int rev_Integer (int);

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

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

 return rev;
}
Please Enter any value
12345
Reverse = 54321

Within this, When the compiler reaches to rev_Integer (Number) line in the main() program, then the compiler will immediately jump to the below function:

int rev_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 allows us to enter any positive integer. Then it will reverse a number using Recursion.

#include <stdio.h>

int Rev_Integer (int );

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

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

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

int Rev_Integer (int n)

In this function, Rev_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, if Number = 459, then the output will be 9

Let’s see the If condition of this reverses a number.

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 as Infinite Loop).

Comments are closed.