Question
Check Whether a Number is Palindrome or Not.
(A palindrome number is a number that remains the same when its digits are reversed)
ENTER A NUMBER
101
101 is a palindrome.
ENTER A NUMBER
1321
1321 is not a palindrome.
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
Java
Python
Java
import java.util.Scanner;
public class Palindrome
{
public static void main(String[] args)
{
int num =0,reversedInteger = 0, remainder, temp=0;
/*The input provided by user is stored in num*/
Scanner sc = new Scanner(System.in);
System.out.println("ENTER A NUMBER");
num = sc.nextInt();
temp= num;
// reversed integer is stored in variable
while( temp > 0 )
{
remainder = temp % 10;
reversedInteger = reversedInteger * 10 + remainder;
temp /= 10;
}
/* palindrome if num and reversedInteger are equal*/
if (num == reversedInteger)
{
System.out.println(num + " is a palindrome.");
}
else
{
System.out.println(num + " is not a palindrome.");
}
}
}
Python
number=int(input("Enter a number:"))
temp=number
reverseOfNumber=0
while(temp>0):
digit=temp%10
reverseOfNumber=reverseOfNumber*10 + digit
temp=temp//10
if(number==reverseOfNumber):
print(number,"is a Palindrome number")
else:
print(number, "is not a Palindrome number")