Question
Print Palindrome number in given range using recursion
ENTER THE LOWER RANGE
1
ENTER THE UPPER RANGE
50
PALINDROME NUMBER BETWEEN 1 AND 50:
1 2 3 4 5 6 7 8 9 11 22 33 44
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
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 lowerRange=0,upperRange=0,reverse=0,i=0;
Scanner sc=new Scanner(System.in);
System.out.println("ENTER THE LOWER RANGE");
lowerRange=sc.nextLong();
System.out.println("ENTER THE UPPER RANGE");
upperRange=sc.nextLong();
System.out.println("PALINDROME NUMBER BETWEEN "+lowerRange+" AND "+upperRange+":");
for(i=lowerRange;i<=upperRange;i++)
{
reverse=FindReverse(i,0);
if(i==reverse)
{
System.out.print(i+" ");
}
}
}
}