Palindrome in C Programming

Written By -

Program for palindrome in C

A palindrome number is a number that remains the same when its digits are reversed. The reverse of the original number and the original number are equal if the number is a palindrome number.

Therefore, to check if a number entered by the user is a palindrome number, we simply need to reverse that number and compare that reversed number with the number originally entered by the user. If both the numbers match, then the number is a palindrome, otherwise it is not.

The code for checking if a number is a palindrome number is:

``````#include <stdio.h>

int  main(){
int  num;
int  reversedNum = 0;
int  remainder;
printf("Enter an integer: ");
scanf("%d", &num);
int  originalNum = num;
while (num != 0)   {
remainder = num % 10;
reversedNum = reversedNum * 10  + remainder;
num = num / 10;
}

if (reversedNum == originalNum){ //if the reversed number is the same as the original number then the original number is a palindrome number
printf("%d is a palindrome number", originalNum);
} else{
printf("%d is not a palindrome number", originalNum);
}
return  0;
}``````

The inputs and outputs for the above code are:

``````Enter an integer: 1111
1111 is a palindrome number

Enter an integer: 1233
1233 is not a palindrome number``````

Algorithm

1. We have created the variable num to store the user input and the variable reversedNum to store the reverse of the number input by the user.
2. The while loop runs until num is not equal to 0.
3. In each iteration, we calculate the remainder when num is divided by 10. The value of num is then reduced by 10 times.
4. At the end of the loop, we get the reversed number.
5. We compare the reversed number with the original number. If these numbers are equal then the number entered by the user is a palindrome, otherwise it is not.

Related Posts:

Online Compilers