How to check a Palindrome Number by C

Profile Picture

saxenadivya859

Saturday, 2024-08-03



To check if a number is a palindrome in C, you need to determine whether the number reads the same forwards and backwards. Here’s a step-by-step approach to achieve this:


  1. Reverse the Number: Reverse the digits of the number and compare it to the original number.
  2. Check Equality: If the reversed number is the same as the original number, then it’s a palindrome.


Palindrome Number Program in C - GeeksforGeeks


Code:

#include <stdio.h>

int main() {
    int num, originalNum, reversedNum = 0, remainder;

    // Input the number from user
    printf("Enter an integer: ");
    scanf("%d", &num);

    // Store the original number to compare later
    originalNum = num;

    // Reverse the number
    while (num != 0) {
        remainder = num % 10;           // Get the last digit
        reversedNum = reversedNum * 10 + remainder; // Append digit to reversed number
        num /= 10;                      // Remove the last digit from num
    }

    // Check if the reversed number is equal to the original number
    if (originalNum == reversedNum) {
        printf("%d is a palindrome.\n", originalNum);
    } else {
        printf("%d is not a palindrome.\n", originalNum);
    }

    return 0;
}


How did you feel about this post?

😍 🙂 😐 😕 😡