Palindrome
Palindrome is a word, number, phrase, or other sequence of characters which reads the same backward as forward, such as madam, racer.
Palindrome numbers are 121, 141, 34543, 343, 131, 48984.
Palindrome number algorithm
- Get the number from user
- Store the number in temporary variable
- Reverse the number
- Compare the temporary number with reversed number
- If both numbers are matches, print palindrome number
- Else print not palindrome number.
Palindrome program in C
Here we take the input from the user and check whether number is palindrome or not.
#include<stdio.h>
void main()
{
int n,rev=0,rem;
printf("Enter the number\n");
scanf("%d",&n);
int t=n;
while(t!=0)
{
rev=rev*10;
rev=rev+t%10;
t=t/10;
}
if (n==rev)
{
printf("%d is palindrome\n",n);
}
else
{
printf("%d is not palindrome\n",n);
}
}
Enter the number
121
121 is palindrome
4683
4683 is not palindrome
Palindrome program in Java
package programs;
import java.util.Scanner;
public class palindrome
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter number");
int n=sc.nextInt();
int rev = 0;
int t=n;
while(n!=0)
{
rev=rev*10;;
rev=rev+t%10;
t=t/10;
}
if(n==rev)
System.out.println("Palindrome");
else
System.out.println("Not Palindrome" );
}
}
Enter number
121
Palindrome
123456
Not Palindrome
Palindrome program in Python
number-int(input("Enter a number")
rev=0
while(number>0):
digit=number%10
rev=rev*10+digit
number=number//10
if(temp==rev):
print("The number is palindrome")
else:
print("Not a palindrome")
Enter a number:
121
The number is palindrome