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.

Advertisement

C Program to Check Palindrome Number

Below is the C program that checks whether a given number is a palindrome or not.

Explanation:

  1. The program begins by declaring the necessary variables num, reversedNum, originalNum, and remainder.
  2. The user is prompted to enter a positive integer using the printf and scanf functions.
  3. The original value of the entered number is stored in the originalNum variable for later comparison.
  4. 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).
  5. Once the loop finishes, the reversed number is stored in the reversedNum variable.
  6. 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:

  1. The user is prompted to enter a positive integer.
  2. The value 12321 is entered.
  3. The program begins the process of reversing the number.
  4. 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).
  5. In the second iteration, the remainder is 1232 % 10, resulting in 2. reversedNum becomes 12, and num becomes 123 (1232 / 10).
  6. The process continues until the num variable becomes 0.
  7. After completing the loop, the reversedNum variable holds the value 12321.
  8. The program then compares originalNum (12321) with reversedNum (12321).
  9. 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.

Share.
Leave A Reply


Exit mobile version