# C program to check if a number is palindrome

Category: C Program

Learn how to write a C program to check if a number is a palindrome. This article provides a detailed explanation and sample code for this fundamental task in C programming.

Checking if a number is a palindrome is a common problem in programming that helps in understanding basic arithmetic operations and control structures. A palindrome number is a number that remains the same when its digits are reversed. This article will guide you through writing a C program to check if a given number is a palindrome, providing a detailed explanation and sample code.

## Steps to Check if a Number is Palindrome

To solve this problem, follow these steps:

1. Input the Number: Read the number from the user.
2. Reverse the Number: Use a loop to reverse the digits of the number.
3. Compare the Original and Reversed Numbers: Check if the reversed number is equal to the original number.
4. Print the Result: Output whether the number is a palindrome.

## Write a C program to check if a number is palindrome

Here's a C program to check if a number is a palindrome:

``````#include <stdio.h>

int main() {
int number, originalNumber, digit, reverse = 0;

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

// Store the original number
originalNumber = number;

// Calculate the reverse of the number
while (number != 0) {
digit = number % 10;      // Extract the last digit
reverse = reverse * 10 + digit;  // Append the digit to the reversed number
number /= 10;             // Remove the last digit
}

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

return 0;
}
``````

Output

``````Enter an integer: 12321
12321 is a palindrome.
``````

### Explanation

1. Input the Number: The program prompts the user to enter an integer. The `scanf` function reads the input number and stores it in the variable `number`.
2. Store the Original Number: The original number is stored in the variable `originalNumber` for later comparison.
3. Reverse the Number:
• The `while` loop continues as long as `number` is not equal to `0`.
• The last digit is extracted using the modulus operator (`%`). For example, if `number` is `12321`, `digit = 12321 % 10` will result in `digit = 1`.
• The extracted digit is appended to the `reverse` variable by multiplying `reverse` by 10 and adding the digit. For the initial iteration, `reverse = 0 * 10 + 1` results in `reverse = 1`.
• The last digit is removed from `number` using integer division (`/`). For example, `number = 12321 / 10` results in `number = 1232`.
4. Compare the Original and Reversed Numbers:
• After reversing the number, the program checks if `originalNumber` is equal to `reverse`.
• If they are equal, the number is a palindrome. Otherwise, it is not.
5. Print the Result: The program prints whether the number is a palindrome to the console using the `printf` function.

### Detailed Steps

• Step 1: Input the Number
• Use `printf` to prompt the user for input.
• Use `scanf` to read the number from the user.
• Step 2: Store the Original Number
• Store the original number in a separate variable for later comparison.
• Step 3: Reverse the Number
• Use a `while` loop to continue processing as long as `number` is not zero.
• Use the modulus operator (`%`) to get the last digit of the number.
• Multiply the `reverse` variable by 10 and add the extracted digit.
• Remove the last digit from the number using integer division (`/`).
• Step 4: Compare the Original and Reversed Numbers
• Check if the original number is equal to the reversed number.
• Step 5: Print the Result
• Use `printf` to display whether the number is a palindrome.

### Example

For an input of `12321`, the program will:

• Extract `1` and append it to `reverse` (reverse = 1).
• Extract `2` and append it to `reverse` (reverse = 12).
• Extract `3` and append it to `reverse` (reverse = 123).
• Extract `2` and append it to `reverse` (reverse = 1232).
• Extract `1` and append it to `reverse` (reverse = 12321).

Since the original number `12321` is equal to the reversed number `12321`, the output will be `12321 is a palindrome.`

For an input of `12345`, the program will:

• Extract `5` and append it to `reverse` (reverse = 5).
• Extract `4` and append it to `reverse` (reverse = 54).
• Extract `3` and append it to `reverse` (reverse = 543).
• Extract `2` and append it to `reverse` (reverse = 5432).
• Extract `1` and append it to `reverse` (reverse = 54321).

Since the original number `12345` is not equal to the reversed number `54321`, the output will be `12345 is not a palindrome.`