# 3 Ways of Reverse a Number in Java

In this Post, we will learn different ways to reverse a number in java.

This is frequently asked question in automation testing  interviews.

Logic 1: Reverse a Number in java using while loop

```package demopkg;

import java.util.Scanner;

public class RevNumberLogic1 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
System.out.println("Enter a number: ");
int num = sc.nextInt();

int rev =0;

while(num!=0) {

rev = rev*10 + num%10;
num = num/10;

}

System.out.println("Reverse of number: " + rev);

}

}

```

Code explanation:

• Using Scanner class we will take the input number from user.
• Before calculating reverse number first we will check number should not equal to 0 in while loop.
• In line no.  17, we will calculate modular of number and add into reverse number. (Initially rev value is 0 ).
• In line no. 18, divide the number with 10 to skip the last digit of number.

Logic 2: Using StringBuffer class

```package demopkg;

import java.util.Scanner;

public class RevNumberLogic2 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
System.out.println("Enter a number: ");
int num = sc.nextInt();

//Using StringBuffer
StringBuffer rev;
StringBuffer sb = new StringBuffer(String.valueOf(num));
rev=sb.reverse();

System.out.println(rev);

}

}
```

Code explanation:

• Using Scanner class we will take the input number from user.
• Using reverse method of StringBuffer class, we can easily reverse the number.
• But at the time of passing number into StringBuffer, using String.valueOf() method we convert number into String.

Logic 3: Using StringBuilder class

```package demopkg;

import java.util.Scanner;

public class RevNumberLogic3 {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number: ");
int num = sc.nextInt();

//Using StringBuilder

StringBuilder sb = new StringBuilder();
sb.append(num);
StringBuilder rev = sb.reverse();
System.out.println(rev);

}
}

```

Code explanation:

• Using Scanner class we will take the input number from user.
• Append that number using append() method of StringBuilder.
• Using reverse() method of StringBuilder we can reverse it.

## Reverse A Number in Java Using Collections:

The reverse() method of the collections class we can reverse the order of specified list.

```import java.util.*;
public class CollectionsReverseExm2 {
public static void main(String[] args) {
//Create a list of Integer
List<Integer>  m = new ArrayList<Integer>();
System.out.println("Original List:- " + m);
Collections.reverse(m);
System.out.println("Modified List:- " + m);
}
}```

Output:

Original List:- [1, 2, 3, 4] Modified List:- [4, 3, 2, 1]

## Reverse a Number in Java using recursion:

```// Java program to reverse
// an integer recursively
class RevNumberUsingRecusion {

// Recursive function to print
// the number in reversed form
public static void Reverse(int num)
{

// base condition to end recursive calls
if (num < 10) {
System.out.println(num);
return;
}

else {

// print the unit digit of the given number
System.out.print(num % 10);

// calling function for remaining number other
// than unit digit
Reverse(num / 10);
}
}

// Main Method code
public static void main(String args[])
{
// number to be reversed
int num = 7654;

System.out.print("Reversed Number: ");

// calling recursive function
// to print the number in
// reversed form
Reverse(num);
}
}
```

Output:

Reversed Number: 4567

Watch this Video:

I hope you have enjoyed this post. Happy Learning 🙂