Check number is Palindrome or not: C Program

0
3130

Palindrome number in C: A palindrome number is a number such that if we reverse it, it will not change. For example some palindrome numbers examples are 121, 212, 12321, -454. To check whether a number is palindrome or not first we reverse it and then compare the number obtained with the original, if both are same then number is palindrome otherwise not. C program for palindrome number is given below.

Formal Definition

Although palindromic numbers are most often considered in the decimal system, the concept of palindromicity can be applied to the natural numbers in any numeral system. Consider a number n > 0 in base b ≥ 2, where it is written in standard notation with k+1 digits ai as:

n=\sum_{i=0}^ka_ib^i

with, as usual, 0 ≤ ai < b for all i and ak ≠ 0. Then n is palindromic if and only if ai = aki for all i. Zero is written 0 in any base and is also palindromic by definition.

Decimal Palindromic numbers

All numbers in base 10 with one digit are palindromic. The number of palindromic numbers with two digits is 9:

{11, 22, 33, 44, 55, 66, 77, 88, 99}.

There are 90 palindromic numbers with three digits (Using the Rule of product: 9 choices for the first digit – which determines the third digit as well – multiplied by 10 choices for the second digit):

{101, 111, 121, 131, 141, 151, 161, 171, 181, 191, …, 909, 919, 929, 939, 949, 959, 969, 979, 989, 999}

Palindrome number algorithm

1. Get the number from user.
2. Reverse it.
3. Compare it with the number entered by the user.
4. If both are same then print palindrome number
5. Else print not a palindrome number.

[message_box title=”Program ” color=”red”]

/*  write a C program to check whether a number is palindrome or not */

/* Written by Utpal Chaudhary */

/*Student of L.D COLEGE OF ENGINEERING,Ahmedabad-15,Gujarat */

#include <stdio.h>
#include <conio.h>

void main()
{
int n, reverse=0, rem,temp;
clrscr();

printf(“Enter an integer: “);
scanf(“%d”, &n);

temp=n;

while(n >0)
{
rem=n%10;
reverse=reverse*10+rem;
n/=10;
}
/* Checking if number entered by user and it’s reverse number is equal. */

if(reverse==temp)
{
printf(“%d is a palindrome.”,temp);
}
else
{
printf(“%d is not a palindrome.”,temp);
}
getch();

}
[/message_box]

[message_box title=”Output” color=”red”]
Enter an integer:121
121 is a palindrome.
[/message_box]

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments