C Program to check Check if a Number is a Palindrome

Subject: PPS (Programming for Problem Solving)

Contributed By: Sanjay

Created At: February 3, 2025

Question:


Write a Check if a Number is a Palindrome.

Explanation Video:

Custom Image

Explanation:

Palindrome:

  • A palindrome is a number that reads the same forward and backward.
  • We reverse the number and compare it with the original.

    Loop Explanation:
  • Extract each digit and reverse the number.
  • Compare original and reversed values.
Source Code:
#include <stdio.h>
int main() {
    int num, reversed = 0, remainder, original;

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

    original = num;  // Store the original number

    while (num != 0) {
        remainder = num % 10;  
        reversed = reversed * 10 + remainder;  
        num /= 10;  
    }

    if (original == reversed)
        printf("%d is a palindrome.\n", original);
    else
        printf("%d is not a palindrome.\n", original);

    return 0;
}
Input:
Input 1 (Palindrome Number):
Enter a number: 121
Input 2 (Not a Palindrome):
Enter a number: 123

Output:
Output 1:
121 is a palindrome.
Output 2:
123 is not a palindrome.
Share this Article & Support Us:
Status
printf('Loading...');