C Program to reverse number and compare(with Algorithm)

In this C program, we will reverse a five-digit number inputted by the user and check whether the original and reversed numbers are equal or not.

We will first take input value by the user and reverse the inputted number. Then we will determine whether the original and reversed number is equal or not.

Before writing this C program, let’s learn about the concept we are gonna use in our program.

Concepts

After taking the values inputted by the user. We will reverse the number with the help of a while loop concept. Then after getting the reversed number, we use the if-else in the C program to check whether the original and reverse number is equal or not.

The concepts of C programming you need to learn before writing the program are:-

  1. while loop
  2. If else

Kindly learn these basic topics before continuing and writing this C program.

2. Algorithm

Here’s the algorithm for writing C program to reverse number inputted by user.

  1. START
  2. Input num
  3. revnum = revnum * 10;
  4. revnum = num % 10 + revnum;
  5. num = num/10;
  6. until num!=0
  7. Write revnum
  8. check revnum is same as num or not
  9. STOP

Keep this algorithm in mind while writing this C program to reverse number.

3. C program to Reverse Number

After going through the algorithm, write the C Program to get input from the user and reverse it to compare it with the inputted number. You can watch the video tutorial or can understand the code given below the video.

#include<stdio.h>
#include<conio.h>
void main()
{
 int num, n, revnum=0;
printf("Enter a five digit number: ");
 scanf("%d", &num);
 n = num;
 while(num!=0)
 {
 revnum = revnum * 10;
revnum = num % 10 + revnum;
num = num/10;
 }
 printf("Reversed Number of %d is %d\n",n, revnum);
if (n==revnum)
 printf("Input Number %d & Reversed Number %d are equal", n, revnum);
 else
 printf("Input Number %d & Reversed Number %d are not equal", n, revnum);
getch();
}

4. Output

After writing the C Program to reverse number and compare, let’s check the output. As we have inputted the value 12321. Let’s have a look at the Output.

groot
groot

Leave a Reply

Your email address will not be published. Required fields are marked *