C program to check whether a number is palindrome or not

Previous Program Next Program

Write a C program to enter any number and check whether given number is palindrome or not using for loop. How to check whether a number is palindrome or not in C programming. Logic to check palindrome number in C program.

Example

Input

Input any number: 121

Output

121 is palindrome

Required knowledge

Basic C programming, If else, For loop

What is Palindrome number?

Palindrome number is such number which when reversed is equal to the original number. For example: 121, 12321, 1001 etc.

Logic to check palindrome number

From the definition of the palindrome number. You must first learn to reverse the number to check palindrome. Here I won't explain the logic to find reverse. You can learn it from below link.

You can divide the logic to check palindrome number in three steps.

  1. Read number from user. Store it in some variable say num.
  2. Find reverse of the number. Store it in some variable say reverse.
  3. Compare original number with reversed number. If both are same then the number is palindrome otherwise not.

Program to check palindrome number

/**
 * C program to check whether a number is palindrome or not
 */

#include <stdio.h>

int main()
{
    int n, num, rev = 0;

    /* Read a number from user */
    printf("Enter any number to check palindrome: ");
    scanf("%d", &n);

    num = n; //Copy original value to num.

    /* Find reverse of n and stores in rev */
    while(n != 0)
    {
        rev = (rev * 10) + (n % 10);
        n = n / 10;
    }

    /* Check if reverse is equal to original num or not */
    if(rev == num)
    {
        printf("%d is palindrome.", num);
    }
    else
    {
        printf("%d is not palindrome.", num);
    }

    return 0;
} 

Advance your skills by learning this program using recursive approach.

Output
Enter any number to check palindrome: 121
121 is palindrome.

Happy coding ;)

You may also like

Previous Program Next Program

Labels: , ,