Palindrome number in C

Created with Sketch.

 

Palindrome number in C

A palindrome number is one that remains the same on reversal. Some examples are 8, 121, 212, 12321, -454. To check if a number is a palindrome or not, we reverse it and compare it with the original number, if both are the same, it’s a palindrome otherwise not.

C program for palindrome number

#include <stdio.h>

int main()
{
int n, r = 0, t;

printf(“Enter a number to check if it’s a palindrome or not\n);
scanf(“%d”, &n);

t = n;

while (t != 0)
{
r = r * 10;
r = r + t%10;
t = t/10;
}

if (n == r)
printf(“%d is a palindrome number.\n, n);
else
printf(“%d isn’t a palindrome number.\n, n);

return 0;
}

Output of the program:
Palindrome number C program output

Function to check palindrome number

int check_palindrome(int n) {
int t, r = 0;

t = n;

while (t != 0) {
r = r * 10;
r = r + t%10;
t = t/10;
}

if (n == r)
return 1;
else
return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *