In the world of programming, palindrome numbers play a significant role due to their unique properties. A palindrome number is a number that remains the same when its digits are reversed. For example, 121, 3443, and 1221 are palindrome numbers. In this article, we will explore how to write a C program to check whether a given number is a palindrome or not. We will provide a step-by-step explanation along with an illustrative example.
C Program to Check Palindrome Number
Below is the C program that checks whether a given number is a palindrome or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include <stdio.h> int main() { int num, reversedNum = 0, originalNum, remainder; printf("Enter a positive integer: "); scanf("%d", &num); originalNum = num; // Reversing the number while (num != 0) { remainder = num % 10; reversedNum = reversedNum * 10 + remainder; num /= 10; } // Checking if the reversed number is equal to the original number if (originalNum == reversedNum) printf("%d is a palindrome.", originalNum); else printf("%d is not a palindrome.", originalNum); return 0; } |
Explanation:
- The program begins by declaring the necessary variables num, reversedNum, originalNum, and remainder.
- The user is prompted to enter a positive integer using the printf and scanf functions.
- The original value of the entered number is stored in the originalNum variable for later comparison.
- The next step involves reversing the number using a while loop. Inside the loop, the last digit of the number (num % 10) is extracted using the modulus operator (%), and it is added to the reversedNum variable after shifting the existing digits one place to the left (reversedNum = reversedNum * 10 + remainder). Then, the last digit is removed from the original number by dividing it by 10 (num /= 10).
- Once the loop finishes, the reversed number is stored in the reversedNum variable.
- Finally, the program compares the originalNum with reversedNum to determine if they are equal. If they are equal, the number is a palindrome and an appropriate message is displayed. Otherwise, it is not a palindrome.
Example:
Let’s consider an example to illustrate the working of the program. Suppose we want to check whether the number 12321 is a palindrome or not. Here’s how the program execution would progress:
- The user is prompted to enter a positive integer.
- The value 12321 is entered.
- The program begins the process of reversing the number.
- In the first iteration of the loop, the remainder is calculated as 12321 % 10, which results in 1. The reversedNum becomes 1, and num becomes 1232 (12321 / 10).
- In the second iteration, the remainder is 1232 % 10, resulting in 2. reversedNum becomes 12, and num becomes 123 (1232 / 10).
- The process continues until the num variable becomes 0.
- After completing the loop, the reversedNum variable holds the value 12321.
- The program then compares originalNum (12321) with reversedNum (12321).
- Since both numbers are equal, the program displays the message “12321 is a palindrome”.
Conclusion
The C program presented in this article allows us to determine whether a given number is a palindrome or not. By understanding the logic and structure of this program, programmers can build upon it to create more complex applications involving palindrome numbers. Palindrome numbers provide an interesting way to explore and practice various programming concepts.