# How do you reverse a number without a loop in Java?

In this program, you will learn how to reverse numbers without a loop using Java. We will use the Recursion method to reverse the numbers. Just simple mathematic operation we will perform to reverse the number. In this program, we are taking the input from predefined as well as from the user.

Reverse mean shifting the first value to last value and shifting last value to first value.

Let’s suppose a user will input a number ‘1234‘. the we will perform reverse operation on it.

## How program will work?

1. First, initialize integer variable like(num).
2. Now create a method/function reverse method.
3. Then pass the variable which we initialize.
4. In that method use the Recursion method.
5. if the variable is less than 10 than print value and breaks condition.
6. else print mod value and pass again remain value in the method.

### Example:

num=1234
rev_num = 0

rev_num = rev_num *10 + num%10 = 4
num = num/10 = 123

rev_num = rev_num *10 + num%10 = 40 + 3 = 43
num = num/10 = 12

rev_num = rev_num *10 + num%10 = 430 + 2 = 432
num = num/10 = 1

rev_num = rev_num *10 + num%10 = 4320 + 1 = 4321
num = num/10 = 0

## Program of reverse number without using a loop in Java. (predefined variable)

```public class reverse_without_loop {
public static void main(String[] args) {
int num = 6548;
reverseMethod(num);
}

private static void reverseMethod(int num) {
if(num<10){
System.out.print(num);
return;
}
else {
System.out.print(num%10);
reverseMethod(num/10);
}
}
}
```

## Program of reverse number without using a loop in Java.(user-defined variable)

```import java.util.Scanner;

public class reverse_without_loop {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
reverseMethod(num);
}

private static void reverseMethod(int num) {
if(num<10){
System.out.print(num);
return;
}
else {
System.out.print(num%10);
reverseMethod(num/10);
}
}
}
```