# C Program to Check Whether a Number is Palindrome

Get Certified in C Programming and Take Your Skills to the Next Level

A palindrome number reads the same backwards as forward. For example, 121 and 454 are palindrome numbers. In this article, we will explore how to write a C program to determine whether a given number is a palindrome or not. We will discuss the algorithm and provide a detailed step-by-step guide, along with code snippets, to help you understand and implement the solution.

### Algorithm to check Palindrome using C:

To check whether a number is a palindrome, we need to compare the digits of the number in a symmetric manner. Here’s the algorithm we’ll follow:

1. Accept the input number from the user.

2. Copy the input number to a separate variable for comparison.

3. Initialize a variable to store the reverse of the number (initially set to 0).

4. Extract the last digit of the number using the modulus operator (%).

5. Multiply the reverse variable by ten and add the extracted digit to it.

6. Divide the original number by 10 to remove the last digit.

7. Repeat steps 4-6 until the original number becomes 0.

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

8. Compare the reverse variable with the initially copied number.

9. If they are equal, the number is a palindrome; otherwise, it is not.

### C Program to Check Whether a Number is a Palindrome:

Now let’s dive into the C code that implements the algorithm explained above:

```#include <stdio.h>
int main() {
int number, reverse = 0, original, remainder;
printf("Enter a number: ");
scanf("%d", &number);
original = number;

while(number!=0){
remainder = number%10;
reverse = reverse*10 + remainder;
number = number/10;
}

if (original == reverse) {
printf("%d is a palindrome number.\n", original);
}
else {
printf("%d is not a palindrome number.\n", original);
}
return 0;
}
```

Explanation:

1. We start by accepting the input number from the user using the `scanf()` function.

2. Then, create variables `reverse`, `original`, and `remainder` to store the reversed number, the original number, and the remainder of each digit, respectively.

3. We copy the original number to the `original` variable for later comparison.

4. Using a `while` loop, we extract the last digit of the number by calculating the remainder with 10.

5. We update the `reverse` variable by multiplying it by ten and adding the extracted digit.

6. We divide the number by 10 to remove the last digit.

7. The loop continues until the original number becomes 0.

8. Finally, we compare the `original` and `reverse` variables to determine whether the number is a palindrome or not. The result is displayed using `printf()`.

### Alternative methods to check palindrome using C Programming Language:

1Â Using an Array

```#include <stdio.h>
int main() {
int number,dataflair, reverse = 0, i, digit[10];
printf("Enter a number: ");
scanf("%d", &number);
dataflair = number;
for (i = 0; number != 0; i++) {
}
digit[i] = number % 10;
number = number / 10;
for (int j = 0; j < i; j++) {
}
reverse = reverse 10 + digit[j];
}
if (dataflair == reverse) {
printf("%d is a palindrome number.\n", dataflair); } else {
printf("%d is not a palindrome number.\n", dataflair);}
return 0;
}```

2Â C Program to check Palindrome Using Recursion

```#include <stdio.h>
int ispalindrome (int dataflair, int original) {
if (dataflair == 0) {
return original;
} else {
}
return ispalindrome (dataflair / 10, original * 10 + number % 10);
}
int main() {
int dataflair;
printf("Enter a number: ");
scanf("%d", &dataflair);
int result = isPalindrome (dataflair, 0);
if (dataflair == result) {
printf("%d is a palindrome number.\n", dataflair); } else {
printf("%d is not a palindrome number.\n", dataflair);}
return 0;
}
```

3 Using a String to find palindrome in C

```#include <stdio.h>
#include <string.h>
int main() {
char str[20];
printf("Enter a number: "); scanf("%s", dataflair);
int length = strlen(dataflair); int ispalindrome = 1;
for (int i = 0; i < length / 2; i++) { if (dataflair[i] != dataflair[length - i - 1]) { ispalindrome = 0;
break;
}
}
if (isPalindrome) {
printf("%s is a palindrome number.\n", dataflair);
} else {
printf("%s is not a palindrome number.\n", dataflair);
}
return 0;
```

These alternative methods demonstrate different approaches to solving the palindrome number problem in C, while still achieving the same result.

Output

Enter a number: 126789
126789 is not a palindrome number.
Enter a number: 96969
96969 is a palindrome number.

### Conclusion

By following the step-by-step guide and examining the code snippets provided, you should now have a clear understanding of how to write a C program to check whether a number is a palindrome. Palindrome numbers are a fascinating concept, and implementing this program showcases how logical thinking and basic programming constructs can be utilized to solve interesting problems.

We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google