April 30, 2022, Learn eTutorial

1496

**A Palindrome number is a number that will be the same even if its digits are reversed**. For a better understanding of this **Palindrome C program example**, we always recommend you to learn the basic topics of C programming listed below:

This C program explains how to reverse a given number and check whether it is a Palindrome or not.

A **Palindrome number is a number that will be the same if we read it in reverse**, which means the number or a string will be the same if we read from backward or forward.

For example, '**121**' is a **Palindrome number**. Again in the case of string '**madam**' or '**civic**' is a **Palindrome string**. So to check for a palindrome number, we have to get the digits in that number in reverse and check if both are the same or not.

In this palindrome C program, we initialize a variable '**rev**' to** zero**. After accepting the number as input, save the number to a **temp** variable, then use a '`while loop`

' until the number is greater than **zero**, Then to extract the last digit from the number, use a **mod** operator with '**10**' to get the digit (**remainder**) from the number.

Add that digit we got as remainder to reverse variable using **rev = rev * 10 + digit**. Then to remove the last digit from the number, divide the number by **10**. Finally, compare the number, and the reversed number and check whether they are the same or not. using the **if condition**. If both the numbers are the same, then print the number as a Palindrome. Else not a Palindrome.

**STEP 1:** Include the Header file to access the Libraries and the built-in functions.

**STEP 2:** Declare and Define the variables using in the C program.

**STEP 3:** Accept the number using `printf`

and `scanf`

built-in functions and save that number in a variable.

**STEP 4:** Store the number in a **temporary** variable to keep the original number safe.

**STEP 5:** Open a `While loop`

until the number greater than **Zero** to make the number Reverse.

**STEP 7:** Use the **Mod** operator with **10** to get the number last Digit and save that in a variable.

**STEP 8:** Add the Digit into the Reversed number variable by using the equation** rev * 10 + Digit;** (we are multiplying the number with **10** to get the added digit position to be corrected.)

**STEP 9:** Divide the number with **10** to remove the last Digit and continue that loop until the number is **zero**.

**STEP 10:** Check the number and reversed number is the same using the** **`if`

condition.

**STEP 11:** If that condition is a match, then print it's a **palindrome** number else print, **not a palindrome**.

` ````
#include <stdio.h>
void main()
{
int num, temp, digit, rev = 0;
printf("Enter an integer\n");
scanf("%d", & num);
temp = num; /* original number is stored at temp */
while (num > 0)
{
digit = num; /* use mod operator to take the last digit of the number */
rev = rev * 10 + digit; /* add the digit to rev *10 to assign the digit to position */
num /= 10; /* divide the number by 10 to remove the last digit */
}
printf("Given number is = %d\n", temp);
printf("Its reverse is = %d\n", rev);
if (temp == rev)
printf("Number is a palindrome\n"); /* check both variables are equal if so it is palindrome else not */
else
printf("Number is not a palindrome\n");
}
```

RUN 1 Enter an integer 12321 Given number is = 12321 Its reverse is = 12321 Number is a palindrome RUN 2 Enter an integer 3456 Given number is = 3456 Its reverse is = 6543 Number is not a palindrome