C Program to check whether the number is a Palindrome

In this blog post, you will learn how to write a C program to check whether the number is a palindrome or not. Before writing the code for the palindrome number I will first explain what the palindrome number is.

 

The primary prerequisites to understanding this C Program:

 

What is a Palindrome Number?

A number that remains the same when the digits are reversed is called a palindrome number. For example, 11, 121, 131, etc; are palindrome numbers. However, 123, 300, etc; are not.

Consider the below image for a better understanding of the palindrome number.

palindrom number

 

 

Steps to check palindrome number in C

Step 1:  Ask the user to enter a numeric number to check for Palindrome in C. Call it the original number.

Step 2: Reverse the original number.

Step 3: Compare the original number with the reverse value.

Step 4: If they matched, then it is a palindrome number. Otherwise, it is not a palindrome number in C programming.

 

Example 1:-

Step 1: Suppose enter number is 121.

Step 2: Reverse the digits of 121. Reversing 121 gives 121.

Step 3: Compare the original number 121 with the reverse number 121.

Step 4: Both original and reverse numbers same so 121 is a palindrome number.

 

Example 2:-

Step 1: Suppose enter number is 75.

Step 2: Reverse the digits of 75. Reversing 75 gives 57.

Step 3: Compare the original number 75 with the reverse number 57.

Step 4: Both original and reverse numbers are not the same so 75 is not a palindrome number.

 

Flowchart of Palindrome Program using while loop:

C Program to check whether the number is a Palindrome

 

 

C Program to check palindrome numbers in C:

Below C program ask the user to enter the value of ‘num’. And after getting the value it checks whether ‘num’ is palindrome or not.

#include<stdio.h>

int main(void)
{
    int num, tmp, rev = 0, rem;

    printf("Enter a number = ");
    scanf("%d", &num);

    tmp = num;

    while(tmp != 0)
    {
        rem = tmp % 10;  // get the last digit from tmp
        rev = rev * 10 + rem;
        tmp /= 10;  // remove the last digit from tmp
    }

    if(num == rev)
    {
        printf("%d is a palindrome number\n", num);
    }

    else
    {
        printf("%d is not palindrome number\n", num);
    }

    return 0;
}

Output1:

Enter a number: 121
121 is palindrome number.

 

Explanation of the C Program to check whether the number is a Palindrome:

Step 1 — Give the number and store it in variable num:

Step 2 — Reverse the Given number:

Step A: Isolate the last digit in the number:

With the help of the % operator at every execution we can extract the last digit of a given number. So, suppose 121 is an integer number. If we perform the modulo division on it with 10, then we get the 1 as a remainder. That means we are successfully able to extract the last digit of a given integer number.

rm = tmp%10;

Step B: Append the last digit to reverse:

Now append the extracted last digit, rm to rev using a formula ((10*rev) + rm). It would look like the below expression,

rev= (rev* 10) + rm;

Step C: Remove the last digit from the number:

Now you have already used the last digit of the given number tmp, so remove this digit. To remove the last digit from a given integer number, you must divide it by 10.

tmp = tmp/10;

The above expression removes the last digit from the tmp; it happens because we are performing an integer division that rounds results down to the nearest integer, for example, 121 / 10 => 12.

Now you need to do the same steps till tmp becomes 0 which means there are no new digits left to be reversed in tmp.

Step 3 — Compare Reverse with original number:

If rev equals the (original number) num then it’s a palindrome number” otherwise it is not a palindrome number.

 

Find a palindrome using the function:

In this example code we will create a function isPalindrome() that will inform number is palindrome or not. That means if the given integer is ‘a’, return 1 if ‘a’ is a palindrome, and 0 otherwise.

Example 1:

Input: x = 11
Output: 1
Explanation: 11 reads as 11 from left to right and from right to left.



Example 2:

Input: x = 21
Output: 0
Explanation: From left to right, it reads 21. 
From right to left, it becomes 12. Therefore it is not a palindrome.

 

/*

C Program to Find a palindrome
using the function:

*/
#include <stdio.h>


int isPalindrome(int a)
{
    int rev = 0, rem = 0;
    int tmp = a;
    while(tmp != 0)
    {
        // get the last digit from tmp
        rem = tmp % 10;
        rev = rev * 10 + rem;
        // remove the last digit from tmp
        tmp /= 10;
    }

    return (rev == a);
}

int main()
{
    int num = 0;
    printf("Enter a number = ");
    scanf("%d", &num);

    //call the function to check palindrome number
    const int palindromeNum = isPalindrome(num);
    if(palindromeNum)
    {
        printf("%d is a palindrome number\n", num);
    }
    else
    {
        printf("%d is not palindrome number\n", num);
    }
    return 0;
}

Output:

Enter a number: 121
121 is palindrome number.

 

Find Palindrome using String Conversion:

In this example code we will create a function isPalindrome() that convert the integer number to a string. The function returns 1 if ‘x’ is a palindrome, otherwise returns 0.

/*
C Program to Find a palindrome
using the String Conversion:
*/
#include <stdio.h>
#include<string.h>

int isPalindrome(int x)
{
    char str[20];
    //assume number is palindrome
    int palindrome = 1;
    sprintf(str, "%d", x);
    const int length = strlen(str);
    for (int i = 0; i < length / 2; ++i)
    {
        if (str[i] != str[length - i - 1])
        {
            //Not palindrome number
            palindrome = 0;
            break;
        }
    }
    return palindrome;

}

int main()
{
    int num = 0;
    printf("Enter a number = ");
    scanf("%d", &num);
    //call the function to check palindrome number
    const int palindromeNum = isPalindrome(num);
    if(palindromeNum)
    {
        printf("%d is a palindrome number\n", num);
    }
    else
    {
        printf("%d is not palindrome number\n", num);
    }
    return 0;
}

 

Recommended Articles for you:

Leave a Reply

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