C Exercise Example30
# C Programming Tutorial: Checking for a 5-Digit Palindrome Number (Exercise 30)
In this tutorial, you will learn how to write a C program to determine whether a given 5-digit integer is a **palindrome**. This is a classic programming exercise that helps developers master basic arithmetic operators, digit extraction, and conditional logic.
---
## Introduction to Palindromes
A **palindrome** is a sequence of characters, numbers, or words that reads the same backward as forward.
For a **5-digit integer** (e.g., `12321`), it is classified as a palindrome if:
* The **ones (units) digit** is equal to the **ten-thousands digit**.
* The **tens digit** is equal to the **thousands digit**.
### Problem Statement
Write a C program that prompts the user to input a 5-digit integer, extracts its individual digits, and checks whether the number is a palindrome.
---
## Program Analysis & Logic
To solve this problem, we need to isolate the individual digits of the 5-digit number. We can achieve this using two fundamental arithmetic operators in C:
1. **Division Operator (`/`)**: Performs integer division, discarding any fractional remainder.
2. **Modulo Operator (`%`)**: Returns the remainder of an integer division.
Let's assume the input number is $x = 12321$:
* **Ten-Thousands Digit (`wan`)**:
$$x / 10000 \implies 12321 / 10000 = 1$$
* **Thousands Digit (`qian`)**:
$$(x \% 10000) / 1000 \implies 2321 / 1000 = 2$$
* **Tens Digit (`shi`)**:
$$(x \% 100) / 10 \implies 21 / 10 = 2$$
* **Ones Digit (`ge`)**:
$$x \% 10 \implies 12321 \% 10 = 1$$
*Note: We do not need to extract the hundreds digit because it sits in the middle and does not affect the symmetry check of a 5-digit palindrome.*
---
## Source Code Implementation
Below is the complete, clean C program to check for a 5-digit palindrome:
```c
#include
int main()
{
long ge, shi, qian, wan, x;
printf("Please enter a 5-digit number: ");
if (scanf("%ld", &x) != 1) {
printf("Invalid input.\n");
return 1;
}
// Extract individual digits
wan = x / 10000; /* Extract the ten-thousands digit */
qian = (x % 10000) / 1000; /* Extract the thousands digit */
shi = (x % 100) / 10; /* Extract the tens digit */
ge = x % 10; /* Extract the ones (units) digit */
// Check palindrome conditions
if (ge == wan && shi == qian) {
printf("%ld is a palindrome.\n", x);
} else {
printf("%ld is NOT a palindrome.\n", x);
}
return 0;
}
```
---
## Sample Output
### Case 1: Palindrome Number
```text
Please enter a 5-digit number: 12321
12321 is a palindrome.
```
### Case 2: Non-Palindrome Number
```text
Please enter a 5-digit number: 12345
12345 is NOT a palindrome.
```
---
## Key Considerations & Best Practices
1. **Input Validation**: The basic solution assumes the user enters a valid 5-digit number. In production environments, you should validate that the input $x$ lies strictly between `10000` and `99999` (or `-99999` and `-10000` if handling negative values).
2. **Data Type Selection**: We use `long` for the variable `x` to prevent integer overflow on systems where standard `int` might be limited to 16 bits (though modern 32/64-bit systems handle 5-digit numbers easily with standard `int`).
3. **Alternative Approach (Reversing the Number)**:
For a more scalable solution that works for numbers of *any* length, you can reverse the entire integer mathematically using a loop:
```c
long original = x, reversed = 0;
while (x > 0) {
reversed = reversed * 10 + (x % 10);
x /= 10;
}
if (original == reversed) { /* Palindrome */ }
```
YouTip