C Program To Check Palindrome number or Not
A Palindromic number (Also Known As a Numeral Palindrome or a Numeric palindrome)
is a number (such as 12321) that remains the same when its digits are reversed
Example :
Given Number : 121
It is a Palindrome Number
Since:
121 remains the same, when its digits are reversed
Example :
Given Number : 123
It is Not a Palindrome Number
Since:
123 do Not remain the same, when its digits are reversed
you can use the following algorithm:
- Read the input number.
- Create a copy of the input number.
- Initialize a variable to store the reverse of the number, initially set to 0.
- While the input of the number is not equal to 0, perform the following steps:
- Get the last digit of the number by using the modulo operator (%).
- Multiply the reverse variable by 10 and add the last digit to it.
- Divide the input number by 10 to remove the last digit.
- Compare the copy number with the reverse variable.
- If they are equal, the number is a palindrome; otherwise, it is not.
- Print the result.
In this algorithm, the input number is processed to reverse its digits by repeatedly extracting
the last digit of the number, multiplying the reverse by 10, and adding the last digit to it.
Then, it checks if the reverse is equal to the input number to determine if it is a Palindrome Number.
Please note that this implementation assumes that the input number is a non-negative integer.
You may need to add additional checks for negative numbers or other cases depending on your
requirements.
Source Code
// C Program To Check Palindrome number or Not ?
#include<stdio.h>
#include<conio.h>
void main()
{
int num , Rd , Rev = 0 , ncopy ;
printf("Enter a number To Check : ");
scanf("%d",&num);
ncopy=num;
while( num!=0 )
{
Rd = num % 10 ;
Rev = Rev * 10 + Rd ;
num = num / 10 ;
}
if( Rev==ncopy )
printf("It is a Palindrome Number . ");
else
printf("It is Not a Palindrome Number . ");
getch();
}
Output
Sample Input :
Enter a Number To Check : 121
Sample Output:
It is a Palindrome Number
Sample Input :
Enter a Number To Check : 124
Sample Output:
It is Not a Palindrome Number
Sample Input :
Enter a Number To Check : 13231
Sample Output:
It is a Palindrome Number
This algorithm takes an input number and reverses it using the modulus operator and division.
It then compares the copy number with the reversed number to determine if it is a palindrome or not.
Data Type | Variable Name | Description |
---|---|---|
int | num | To Store an Integer That You Want To Check |
int | Rd | To Store Last Digit of the Number During Loop |
int | Rev | To Store Reversed Number |
int | ncopy | To Store Copy of Input Number |
Looping Related Programs
- C Program to Check Perfect Number or Not
- C Program to Check Prime Number or Not
- C Program to Check Twin Prime Number or Not
- C Program to Check Duck Number or Not
- C Program to Check Niven Or Harshad Number or Not
- C Program to Check Spy Number or Not
- C Program to Check Neon Number or Not
- C Program to Check Armstrong Number or Not
- C Program to Check Strong (Krishnamurthy)Number or Not
- C Program to Check Happy Number or Not
- C Program to Check Automorphic Number or Not