C Program to Reverse a Given Number using While loop, Do-while, For loop
ADVERTISEMENTS
C program to reverse a given number using a while loop, do-while, and for loop.
In this article, you will learn how to make the reverse of a given number by using programming loops like while, do-while, and for.
EXAMPLE:
1234
==> 4321
Example
INPUT: 1234
Output Reversed Number: 4321
You should know about the following topics in C programming to understand this program:
- While loop in C
- Do-while loop in C
- For loop in C
1 - Reverse a Given Number using While loop
// C Program to Reverse a Given Number using While loop
#include <stdio.h>
// It's the main function of the program
int main()
{
int num, rev = 0, rem, original;
// Step-1 Take an input from the User
printf("INPUT: ");
scanf("%d", &num);
// Step-2 Store the input value into the `original` variable
original = num;
// Step-3 Iterate over the input number using the loop
while (num != 0)
{
rem = num % 10;
rev = rev * 10 + rem;
num /= 10;
}
// Step-4 Check if the reminder of original divided by 10 is equal to Zero
if (original % 10 == 0)
{
printf("\nOutput Reversed Number: %d", rev);
while (original % 10 == 0)
{
printf("0");
original /= 10;
}
}
// Step-5 If a reminder of the original is not equal to Zero
else
{
printf("\nOutput Reversed Number: %d", rev);
}
return 0;
}
Output
INPUT: 1234
Output Reversed Number: 4321
2 - Reverse a Given Number using Do-while loop
// C Program to Reverse a Given Number using Do-while loop
#include <stdio.h>
// It's the main function of the program
int main()
{
int num, rev = 0, rem, original;
// Step-1 Take an input from the User
printf("INPUT: ");
scanf("%d", &num);
// Step-2 Store the input value into the `original` variable
original = num;
// Step-3 Iterate over the input number using the loop
do
{
rem = num % 10;
rev = rev * 10 + rem;
num /= 10;
} while (num != 0);
// Step-4 Check if the reminder of original divided by 10 is equal to Zero
if (original % 10 == 0)
{
printf("\nOutput Reversed Number: %d", rev);
do
{
printf("0");
original /= 10;
} while (original % 10 == 0);
}
// Step-5 If a reminder of the original is not equal to Zero
else
{
printf("\nOutput Reversed Number: %d", rev);
}
return 0;
}
3 - Reverse a Given Number using For loop
// C Program to Reverse a Given Number using For loop
#include <stdio.h>
// It's the main function of the program
int main()
{
int num, rev = 0, rem, original;
// Step-1 Take an input from the User
printf("INPUT: ");
scanf("%d", &num);
// Step-2 Store the input value into the `original` variable
original = num;
// Step-3 Iterate over the input number using the loop
for (int n = num; n != 0; n /= 10)
{
rem = n % 10;
rev = rev * 10 + rem;
}
// Step-4 Check if the reminder of original divided by 10 is equal to Zero
if (original % 10 == 0)
{
printf("\nOutput Reversed Number: %d", rev);
for (int o = original; o % 10 == 0; o /= 10)
printf("0");
}
// Step-5 If a reminder of the original is not equal to Zero
else
{
printf("\nOutput Reversed Number: %d", rev);
}
return 0;
}
Output
INPUT: 9876
Output Reversed Number: 6789