Q14. Write a code to check whether the given number is palindrome or not
Palindrome Number:- A Palindrome Number is a number that remains same when its digits are reversed or we can say that the Palindrome number is a number that is symmetric.
For Example:- 121
So as we can see that the number 121 remains the same when it is reversed so it is a palindrome number.
Algorithm for Palindrome Number
START
Step 1 : Get the number to check for palindrome
Step 2 : Hold the number in temporary variable
Step 3 : Reverse the number
Step 4 : Compare the temporary number with reversed number
Step 5 : If both numbers are same, print “palindrome number”
Step 6 : Else print “not palindrome number”
STOP
Code for Palindrome Number
#include <stdio.h>
int main() {
int N=121, rev = 0, rem, new_num;
new_num = N;
// reversed integer is stored in reversedN
while (N != 0) {
rem = N % 10;
rev = rev * 10 + rem;
N /= 10;
}
// palindrome if orignalN and reversedN are equal
if (new_num == rev)
printf(“%d is a palindrome number.”, new_num);
else
printf(“%d is not a palindrome number.”, new_num);
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int n, num=121, digit, rev = 0;
n = num;
do
{
digit = num % 10;
rev = (rev * 10) + digit;
num = num / 10;
} while (num != 0);
if (n == rev)
cout << ” The number “<< n<<” is a palindrome.”;
else
cout << ” The number “<< n<<” is not a palindrome.”;
return 0;
}
public class LFC {
public static void main(String[] args) {
int num = 121, rev = 0, rem, new_num;
new_num = num;
// reversed integer is stored in variable
while( num != 0 )
{
rem = num % 10;
rev = rev * 10 + rem;
num /= 10;
}
// palindrome if new_num and rev are equal
if (new_num == rev)
System.out.println(new_num + ” is a palindrome.”);
else
System.out.parintln(new_num + ” is not a palindrome.”);
}
}
n=121
temp=n
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
if(temp==rev):
print(“The number {0} is palindrome number”.format(temp))
else:
print(“The number {0} is palindrome number”.format(temp))
function palindrome($n){
$number = $n;
$sum = 0;
while(floor($number)) {
$rem = $number % 10;
$sum = $sum * 10 + $rem;
$number = $number/10;
}
return $sum;
}
$input = 121;
$num = palindrome($input);
if($input==$num){
echo “The number $input is a Palindrome number”;
} else {
echo “The number $input is not a Palindrome number”;
}
Output
The number 121 is a Palindrome number
Recommended Programs
Program to find factorial of a number
Program to count number of digits in a number