Last Updated on June 26, 2024 by Abhishek Sharma

A palindrome is a number (or a word) that reads the same forwards and backwards. For example, the numbers 121 and 1331 are palindromes, while 123 and 1234 are not. In computer programming, checking if a number is a palindrome is a common problem that helps to illustrate fundamental concepts such as loops, conditionals, and number manipulation. In C programming, this involves reversing the digits of the number and comparing the reversed number to the original. This article will guide you through the process of checking if a number is a palindrome using the C programming language, providing insights into the logic and coding techniques involved.

In this article, we will explore the concept of palindrome numbers and provide a step-by-step guide on how to determine whether a number is a palindrome or not using the C programming language. We will delve into various approaches and algorithms, allowing both beginners and experienced programmers to gain a deeper understanding of this fascinating problem.

## How We can Check the Number is Palindrome or Not in C?

A number that remains the same after it is flipped around is known as a palindrome. Palindromes include the numbers 121, 34543, 343, 131, and 48984.

A number is said to be a palindrome if it reads the same forward as it does backward when its last digit is the same as the first digit, second digit is the same as the second-last digit, and so on. In conclusion, a number is a palindrome if the outcome of reversing its digits—reversing the sequence of the digits—is the same as the outcome of utilizing the original number.

### Algorithm for Number is Palindrome or Not in C

Let’s now examine the standard procedure for determining if a number is a palindrome or not:

- Step 1. First, check to see if the user-provided integer N is a palindrome.
- Step 2. Create a duplicate of the user-supplied number and assign it to a temporary variable (temp = N) in step two.
- Step 3. In step 3, Create two new variables, digit and reverse, and set their initial values to represent, respectively, the number’s digits and reverse.
- Step 4. Initialize a while loop in step four.
- Step 5. Use the modulus operator in step 5 to eliminate the final digit from the specified integer (N%10).
- Step 6. In step 6, After multiplying the past value of the reverse variable by 10, add this number to the reverse variable. The backward multiplied by 10 plus the digit equals the reverse.(Reverse: Reverse * 10 + digit)
- Step 7. In step 7, In order to access the next digit from the left side, the variable n needs to be updated with a decrement statement. (n = n / 10)
- Step 8. For step 8, Continue with steps 5 through 7 until N is more than zero (N>0).
- Step 9. Step 9: Finish

## Dry Run of Palindrome Number in C

Let’s follow the values of these three variables for the input number 1991 during each iteration of the while loop to better understand how the iterative method for testing palindrome in C functions.

**Initially:**

- N = 1991
- Reverse = 0
- temp = 1991

**Repetition 1:**

- N = 199
- Reverse = 1
- temp = 1991

**Repetition 2:**

- N = 19
- Reverse = 19
- temp = 1991

**Repetition 3:**

- N = 1
- Reverse = 199
- temp = 1991

**Repetition 4:**

- N = 0
- Reverse = 1991
- temp = 1991

Here, we can see that a digit from the input number is subtracted and stored in the reverse variable after each iteration. Once all of the digits have been saved in the reverse variable in reverse order, the loop ends. To determine if the input number is a palindrome or not, the reversed number is compared to the number kept in the temporary variable.

As a result, after applying the aforementioned procedure, the reverse variable will retain the input number’s inverse, and the temp variable will hold the input number’s original value. Let’s see how this iterative approach to palindrome number verification may be applied in C.

Code Implementation

#include <stdio.h> int main() { int N=1991, digit, reverse = 0, temp; temp = N; //loop to find reverse number while(temp != 0) { digit = temp % 10; reverse = reverse * 10 + digit; temp /= 10; }; if (N == reverse) printf("%d is Palindrome", N); else printf("%d is not a Palindrome", N); }

**Output:**

`1991 is Palindrome`

To determine if a number is a palindrome or not, the input number 1991 is used. Iterative C is used by the program to determine whether a number is a palindrome. In this example, we can observe that reversing the number 1991 results in the same result: 1991, the original number. The program’s output reveals that the number 1991 is a palindrome.

There is one more method to check number is palindrome or not by Mathematical technique using recursion

## Mathematical Technique using Recursion For Number is Palindrome or Not in C Programming Language

We can also check if the number is palindrome or not using the recursive method.

In this, we can see that a digit from the input number is subtracted and stored in the reverse variable after each call. Once all of the digits have been saved in the reverse variable in reverse order, the loop ends. To determine if the input number is a palindrome or not, the reversed number is compared to the number kept in the temporary variable.

### Code Implementation

#include int ReverseNumber(int N, int rev){ if(N == 0) return rev; int rem = N % 10; rev = rev * 10 + rem; return ReverseNumber(N / 10, rev); } int main () { int N, reverse = 0; N=1991; if (ReverseNumber(N, reverse) == N) printf("%d is Palindrome", N); else printf("%d is not a Palindrome", N); }

**Output**

`1991 is Palindrome`

**Conclusion**

Checking if a number is a palindrome in C is a straightforward exercise that reinforces essential programming skills. By reversing the digits of a number and comparing it to the original, we can determine if the number reads the same forwards and backwards. This process involves basic operations such as loops, conditionals, and arithmetic manipulations, making it a valuable problem for beginners to practice and understand core programming concepts. As you advance in your programming journey, solving such problems will build a strong foundation for tackling more complex challenges.

## FAQ Related to Number is Palindrome or Not in C

FAQ Related to Number is Palindrome or Not in C are:

**1. What is a palindrome number?**

A palindrome number is a number that remains the same when its digits are reversed. Examples include 121, 1331, and 12321.

**2. How can we check if a number is a palindrome in C?**

To check if a number is a palindrome in C:

- Reverse the digits of the number.
- Compare the reversed number with the original number.
- If they are the same, the number is a palindrome; otherwise, it is not.

**3. What is the basic algorithm to check for a palindrome number in C?**

The basic algorithm involves:

- Storing the original number.
- Reversing the digits of the number.
- Comparing the reversed number with the original number to determine if they are identical.

**4. Can negative numbers be palindromes?**

By definition, palindromes are typically considered only in the context of non-negative integers, as the concept generally applies to the symmetry of digits. However, if considering the entire number including the negative sign, negative numbers are not palindromes because the sign would not be symmetrical.

**5. What are some real-world applications of palindrome checks?**

Palindrome checks can be used in various real-world applications such as:

- Validating data formats (e.g., certain IDs or serial numbers).
- In computer science, for algorithms that require symmetry checks.
- In cryptographic algorithms and checksums to verify data integrity.