How to Write a C Program to Reverse a Number using While Loop, Functions, and Recursion? For this, first, find the remainder by dividing the number by 10.
The below program allows the user to enter any positive integer. Next, this C program use the while loop to traverse the digit sin a number and will reverse a number.
#include <stdio.h> int main(void) { int n, rd, rs = 0; printf("Please Enter any Number to Reverse = "); scanf("%d", & n); while (n > 0) { rd = n %10; rs = rs * 10+ rd; n = n /10; } printf(“Reverse of entered Number is = %d\n", rs); return 0; }
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 Program to Reverse a Number example, User Entered value: n = 1456 and rs = 0
First Iteration
rd = n %10 = 1456%10 = 6
rs = rs *10 + rd
rs = 0 * 10 + 6 => 0 + 6 => 6
n = n /10 = 1456 /10 => 145
Second Iteration: From the first Iteration of this C Program, the values of both have changed as Number =145 and Reverse = 6
rd = 145 % 10 = 5
rs = 6 * 10 + 5 => 60 + 5 => 65
n = 145 /10 => 14
Third Iteration: From the Second Iteration of this program, the values of both changed to 14 and 65
rd = 14 % 10 = 4
rs = 65 * 10 + 4 => 650 + 4 => 654
n = 14 / 10 = 1
Fourth Iteration: From the third Iteration, Number = 1 and Reverse = 654
rd = 1 %10 = 1
rs = 654 * 10 + 1 => 6540 + 1 => 6541
n = 1/ 10 => 0
Here, For the next iteration, n = 0. So, the C Programming while loop condition will fail
C program to Reverse a Number using For loop
This example uses the for loop to iterate the individual digits in a number and reverse them using the logic we used in the while loop example.
#include <stdio.h> int main(void) { int n, rd, rs = 0; printf("Please Enter any Number = "); scanf("%d", &n); for(;n > 0; n = n /10){ rd = n % 10; rs = rs * 10+ rd; } printf("Reverse of entered = %d\n", rs); return 0; }
C Program to Reverse a Number Using Functions
This program allows the user to enter any positive integer. Then, this program reverses a number using Functions
#include <stdio.h> int rev_Integer (int); int main(void) { int Number, rev = 0; printf("Please Enter any number = "); 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; }
Within this, When the compiler reaches the rev_Integer (Number) line in the main() program, 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 the 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(void) { 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 int Rem, rev = 0; if (n > 0) { Rem = n %10; rev = rev *10+ Rem; Rev_Integer (n /10); } return rev; }
In this program, When the compiler reaches the 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, the Rev_Integer (n / 10); statement will help to call the function Recursively with the updated value. If you miss this statement, it will terminate after completing the first line. For example, if Number = 459, then the output will be 9
Let’s see the If condition.
if (n > 0), the code will check whether the number is greater than 0. For Recursive functions, placing a condition before using the function recursively is crucial. Otherwise, we end up in infinite execution (Same as Infinite Loop).
Comments are closed.