Write a C program to check whether a number is a palindrome

Description: Take a number as input. Print if it is a palindrome (it reads the same from both sides), like 121.

Hint:

  • Reverse the number, and compare it with the original.

C program to check a palindrome number

#include <stdio.h>

int main(void) {
    int n, original, reversed = 0;

    printf("Enter a number: ");
    scanf("%d", &n);
    original = n;

    while (n > 0) {
        reversed = reversed * 10 + n % 10;
        n /= 10;
    }

    if (original == reversed) {
        printf("%d is a palindrome\n", original);
    } else {
        printf("%d is not a palindrome\n", original);
    }
    return 0;
}

Output:

Output of the above program is

Enter a number: 121
121 is a palindrome

How it works

  • We keep a copy in original, because the loop changes n.
  • 121 reversed is 121, so both are equal.

Read Reverse a Number.


Learn more about the similar topics:
Tutorials
C If Else Statement
C Else If Ladder and Nested If
C Switch Statement
C While Loop
C Do While Loop
C For Loop
C Nested Loops
Exercises & Assignments
Write a C program to check whether a number is even or odd
Write a C program to check whether a user is eligible to vote or not
Write a C program to check whether an alphabet is vowel or consonant
Write a C program to find the maximum number between three numbers
Write a C program to check whether a number is positive, negative or zero
Write a C program to calculate an electricity bill
Write a C program to print 1 to 10 numbers using a while loop
Interview Questions & Answers
What is the difference between while and do while loop in C?
What is the difference between break and continue in C?
How to check if a number is prime in C?
How to find the factorial of a number in C?