Question
Check whether a number is Palindrome number using recursion
(A palindrome number is a number that remains the same when its digits are reversed)
ENTER THE NUMBER
121
121 IS A PALINDROME NUMBER
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 PalindromeNumber
{
public static long FindReverse(long num,long rev)
{
if(num<=0)
{
return rev;
}
else
{
rev=rev*10+num%10;
return FindReverse(num/10,rev);
}
}
public static void main()
{
long num=0,reverse=0;
Scanner sc=new Scanner(System.in);
System.out.println("ENTER THE NUMBER");
num=sc.nextLong();
reverse=FindReverse(num,0);
if(num==reverse)
{
System.out.println(num+" IS A PALINDROME NUMBER");
}
else
{
System.out.println(num+" IS NOT A PALINDROME NUMBER");
}
}
}
Python
def reverseOfNumber(num,rev=0):
if(num<=0):
return rev
else:
digit=num%10
rev=rev*10+digit
return reverseOfNumber(num//10,rev)
number=int(input("Enter a number:"))
reversedNumber=reverseOfNumber(number)
if(number==reverseOfNumber):
print(number,"is a Palindrome number")
else:
print(number, "is not a Palindrome number")