codescracker
c

C Program to Check Palindrome Number



« Previous Program Next Program »

In this article, you will learn and get code to check whether the given number by user (at run-time) is a palindrome number or not. But before going to the program, let's first understand, which number can be called as palindrome.

What is a Palindrome Number ?

If a number is equal to its reverse is called as a palindrome number. For example, 12321 is a palindrome number, because its reverse (12321) is equal to the number itself. Whereas 12345 is not a palindrome number because its reverse (54321) is not equal to the number itself.

Check Palindrome or Not

Now let's create a program that checks for a Palindrome number.

// C Program to Check Palindrome Number or Not
// ----codescracker.com----

#include<stdio.h>
#include<conio.h>
int main()
{
    int num, rev=0, rem, temp;
    printf("Enter a Number: ");
    scanf("%d", &num);
    temp = num;
    while(temp>0)
    {
        rem = temp%10;
        rev = (rev*10)+rem;
        temp = temp/10;
    }
    if(rev==num)
        printf("\nIt's a Palindrome Number");
    else
        printf("\nIt's not a Palindrome Number");
    getch();
    return 0;
}

This program was build and run under Code::Blocks IDE. Here is its sample run:

check palindrome number in c

Now supply any number say 12321 as input and press ENTER key to see the following output:

c program check palindrome or not

As already told that if a reverse of a number is equal to the original number, then it will be a palindrome number. Therefore, we have just reverseed the number and compares it with original. If it is equal then print it as a palindrome, otherwise print it as not a palindrome using if-else statement

Check Palindrome Number using for Loop

Now let's create the same purpose program using for Loop

// Check Palindrome Number using for Loop
// ----codescracker.com----

#include<stdio.h>
#include<conio.h>
int main()
{
    int num, rev=0, rem, temp;
    printf("Enter a Number: ");
    scanf("%d", &num);
    for(temp = num; temp>0; temp=temp/10)
    {
        rem = temp%10;
        rev = (rev*10)+rem;
    }
    if(rev==num)
        printf("\nIt's a Palindrome Number");
    else
        printf("\nIt's not a Palindrome Number");
    getch();
    return 0;
}

It will produce the same output as of previous program. Here is it sample run, if user enters a number whose reverse it not equal to original one.

c palindrome or not

Same Program in Other Languages


« Previous Program Next Program »