This is a Java Program to Find Reverse of a Number using Recursion.
Enter any integer number as an input. After that we count the number of digits in the given input. The given number along with length of that number is passed to the other function where by using recursion we get the reverse of a number as an output.
Here is the source code of the Java Program to Find Reverse of a Number using Recursion. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
import static java.lang.StrictMath.pow;
import java.util.Scanner;
public class Reverse_Recursion
{
public static void main(String[] args)
{
int n, count = 0, m;
Scanner s = new Scanner(System.in);
System.out.print("Enter the number:");
n = s.nextInt();
m = n;
while(m > 0)
{
count++;
m = m / 10;
}
Reverse_Recursion obj = new Reverse_Recursion();
int a = obj.reverse(n, count);
System.out.println("Reverse:"+a);
}
int reverse(int x, int length)
{
if(length == 1)
{
return x;
}
else
{
int b = x % 10;
x = x / 10;
return (int) ((b * pow(10, length - 1)) + reverse(x, --length));
}
}
}
Output:
$ javac Reverse_Recursion.java $ java Reverse_Recursion Enter the number:467 Reverse:764
Sanfoundry Global Education & Learning Series - 1000 Java Programs.
advertisement
Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.
Related Posts:
- Apply for Java Internship
- Apply for Computer Science Internship
- Practice BCA MCQs
- Practice Information Technology MCQs
- Check Programming Books