A Palindrome Number is a number that remains the same when its digits are reversed. For example, 121 and 12321 are palindrome numbers, while 123 and 456 are not.

Logic to Check if a Number is Palindrome:

  1. Reverse the number using the same method as reversing a number.
  2. Compare the reversed number with the original number.
  3. If they are the same, the number is a palindrome; otherwise, it is not.

Program:

Here’s a simple C program to check whether a number is a palindrome:

#include <stdio.h> int main() { int num, reversed = 0, remainder, original; // Input a number from the user printf("Enter an integer: "); scanf("%d", &num); original = num; // Store the original number // While loop to reverse the digits of the number while (num != 0) { remainder = num % 10; // Extract the last digit reversed = reversed * 10 + remainder; // Build the reversed number num = num / 10; // Remove the last digit from the original number } // Check if the original number and the reversed number are the same if (original == reversed) { printf("%d is a palindrome.\n", original); } else { printf("%d is not a palindrome.\n", original); } return 0; }

Explanation:

  1. Variables:

    • int num: This variable holds the number input by the user.
    • int reversed: This variable will store the reversed number, initialized to 0.
    • int remainder: This variable is used to store the last digit of the original number.
    • int original: This variable stores the original number to compare later.
  2. Input:

    • The user is prompted to enter a number, which is stored in num.
  3. Reversing Logic:

    • The program enters a while loop that runs until num is not equal to 0.
    • Inside the loop:
      • remainder = num % 10; extracts the last digit of num.
      • reversed = reversed * 10 + remainder; builds the reversed number.
      • num = num / 10; removes the last digit from num.
  4. Comparison:

    • After the loop, the program checks if the original number (original) is equal to the reversed number (reversed).
    • If they are equal, the number is a palindrome, and the program prints that it is a palindrome. Otherwise, it prints that it is not a palindrome.

Sample Output:

Example 1:

Enter an integer: 121 121 is a palindrome.

Example 2:

Enter an integer: 12345 12345 is not a palindrome.

Key Points:

  • Reversing Logic: The same technique is used to reverse the number as in the previous example.
  • Comparison: The program compares the reversed number with the original number to determine if it is a palindrome.
  • This program effectively demonstrates basic C concepts, including loops, conditionals, and arithmetic operations.