C Program To Check Palindrome Number Using For Loop
A palindrome number reads the same forwards and backward. For example, 121, 343, and 5005 are palindrome numbers. In this article, you will learn how to write a C program to determine if a given integer is a palindrome using a for loop.
Problem Statement
Checking if a number is a palindrome involves reversing the given number and then comparing the reversed number with the original. If they match, the number is a palindrome. The challenge lies in efficiently reversing the digits of an integer using C's arithmetic operations and controlling the process with a for loop.
Example
Consider the number 121:
- Original number: 121
- Reversed number: 121
- Since original == reversed, 121 is a palindrome.
Consider the number 123:
- Original number: 123
- Reversed number: 321
- Since original != reversed, 123 is not a palindrome.
Background & Knowledge Prerequisites
To understand this program, readers should be familiar with:
- C Basics: Variables, data types (especially
int). - Arithmetic Operators: Modulo (
%) for getting the last digit and division (/) for removing the last digit. - Looping Constructs: The
forloop and its structure (initialization, condition, increment/decrement).
Use Cases or Case Studies
Palindrome checks are useful in various scenarios:
- Data Validation: Ensuring certain IDs or codes follow a palindrome pattern.
- Educational Programming: A common exercise to teach logical thinking and loop usage.
- Recreational Mathematics: Used in puzzles and number theory explorations.
- Security (Niche): In some highly specific, unconventional cryptographic or hashing algorithms, palindrome properties might be leveraged.
- Text Processing (Extended Concept): While this article focuses on numbers, the concept extends to strings (e.g., "madam" is a palindrome string) for data cleaning or pattern recognition.
Solution Approaches
This section details how to check for a palindrome number using a for loop.
Approach 1: Reversing a Number using a For Loop
This approach involves iteratively extracting the last digit of the number, building a reversed number, and finally comparing it with the original.
- Summary: Iteratively extracts digits from the input number, constructing its reverse within a
forloop, and then compares it to the original number.
// Palindrome Number Check using For Loop
#include <stdio.h>
int main() {
int num, originalNum, reversedNum = 0, remainder;
// Step 1: Prompt user for input
printf("Enter an integer: ");
scanf("%d", &num);
originalNum = num; // Store the original number for comparison later
// Step 2: Reverse the number using a for loop
// The loop continues as long as 'num' is greater than 0.
// In each iteration, the last digit is extracted and appended to reversedNum.
// 'num' is then updated by removing its last digit.
for (; num > 0; num /= 10) {
remainder = num % 10; // Get the last digit
reversedNum = reversedNum * 10 + remainder; // Build the reversed number
}
// Step 3: Compare the original number with the reversed number
if (originalNum == reversedNum) {
printf("%d is a palindrome number.\\n", originalNum);
} else {
printf("%d is not a palindrome number.\\n", originalNum);
}
return 0;
}
- Sample Output:
Enter an integer: 121 121 is a palindrome number.
Enter an integer: 1234
1234 is not a palindrome number.
Enter an integer: 5005
5005 is a palindrome number.
- Stepwise Explanation:
- Initialization: Declare variables
num(to store user input and be modified),originalNum(to keep a copy of the originalnum),reversedNum(initialized to 0, to build the reversed number), andremainder(to store the last digit). - Input: The program prompts the user to enter an integer and stores it in
num.originalNumis assigned the value ofnumso that the original number is preserved. - For Loop:
- The
forloop is initialized without an explicit initial expression (asnumis already set). - Condition (
num > 0): The loop continues as long asnumis greater than 0, meaning there are still digits to process. - Increment/Decrement (
num /= 10): After each iteration,numis divided by 10. This effectively removes the last digit fromnum. - Inside the loop:
-
remainder = num % 10;: The modulo operator (%) gives the last digit ofnum. -
reversedNum = reversedNum * 10 + remainder;: This line constructs the reversed number. Each extractedremainderis added toreversedNumafterreversedNumis multiplied by 10 (shifting its existing digits to the left).
- Comparison: After the loop finishes (when
numbecomes 0),originalNum(the preserved input) is compared withreversedNum. - Output: Based on the comparison, a message is printed indicating whether the entered number is a palindrome or not.
Conclusion
Checking for palindrome numbers is a fundamental problem in programming that reinforces understanding of loops and arithmetic operations. The for loop provides a concise way to reverse an integer by repeatedly extracting its last digit and constructing a new number.
Summary
- A palindrome number reads the same forwards and backward (e.g., 121, 5005).
- To check if a number is a palindrome, you must reverse it and compare it to the original.
- The
forloop can effectively reverse an integer by repeatedly using the modulo operator (%) to get the last digit and integer division (/) to remove it. - A copy of the original number (
originalNum) is essential for the final comparison, as thenumvariable is modified during the reversal process.