Question
Print reverse of a number using recursion
ENTER THE NUMBER
5124
Reverse of 5124 is 4215
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 num=0,reverse=0;
Scanner sc=new Scanner(System.in);
System.out.println("ENTER THE NUMBER");
num=sc.nextLong();
reverse=FindReverse(num,0);
System.out.println("Reverse of "+num+" is "+reverse);
}
}