C program to check whether a number is equal to its reverse

In this article, you will learn about checking whether the number given by the user (at run-time) is equal to its reverse or not.

Check if a number is equal to its reverse in C

To check whether the reverse of a number is equal to its original or not in C programming, you have to ask the user to enter the number, then reverse that number. Check whether the reverse value is equal to the original value or not, as shown in the program given below:

#include<stdio.h>
#include<conio.h>
int main()
{
    int num, orig, rev=0, rem;
    printf("Enter a Number: ");
    scanf("%d", &num);
    orig = num;
    while(num>0)
    {
        rem = num%10;
        rev = (rev*10)+rem;
        num = num/10;
    }
    if(orig==rev)
        printf("\nThis number is equal to its Reverse");
    else
        printf("\nThis number is not equal to its Reverse");
    getch();
    return 0;
}

This program was compiled and executed using the Code::Blocks IDE. Here is the first snapshot of a sample run:

c program reverse original

Now supply any number, say 565, and press ENTER to see the output as shown in the snapshot given below:

c check reverse equal original not

As the reverse of 565 is the number itself, you will see the message as given in the previous output.

Before reversing the given number, initialize its value to any variable, say orig. So that you can compare it to the original after reversing the number. Here is another sample run. Let's suppose the user enters 422 as input this time:

reverse original program c

Because 422 is not equal to its reverse, you will see the message as shown in the previous snapshot. The reverse of 422 will be 224. And these two numbers are not equal to each other.

The same program in different languages

C Quiz


« Previous Program Next Program »


Liked this post? Share it!