Question
Print reverse of a number in given range using recursion
ENTER THE LOWER RANGE
25
ENTER THE UPPER RANGE
35
Reverse of 25 is 52
Reverse of 26 is 62
Reverse of 27 is 72
Reverse of 28 is 82
Reverse of 29 is 92
Reverse of 30 is 3
Reverse of 31 is 13
Reverse of 32 is 23
Reverse of 33 is 33
Reverse of 34 is 43
Reverse of 35 is 53
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class Reverse
{
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();
for(i=lowerRange;i<=upperRange;i++)
{
reverse=findReverse(i,0);
System.out.println("Reverse of "+i+" is "+reverse);
}
}
}