Reverse a Number in Java

How to Reverse a Number in Java by writing a Program to Reverse a Number using While Loop, For Loop, Built-in reverse function, Functions and Recursion.

Program to Reverse a Number in Java Using While Loop

This Java program allows the user to enter any positive integer, and then it will reverse a number using While Loop

package SimplerPrograms;

import java.util.Scanner;

public class ReverseNumberUsingWhile {
	private static Scanner sc;
	
	public static void main(String[] args) {
		int Number, Reminder, Reverse = 0;
		sc = new Scanner(System.in);
		
		System.out.println("\n Please Enter any Number you want to Reverse : ");
		Number = sc.nextInt();
		
		while(Number > 0) {
			Reminder = Number %10;
			Reverse = Reverse * 10 + Reminder;
			Number = Number /10;
		}
		System.out.format("\n Reverse of entered number is = %d\n", Reverse);
	}
}
Reverse a Number in Java Using While Loop

The first System.out.println statement will print the statement inside the double Quotes. And next we are assigning the user entered value to integer variable (Number)

Next line, we used the Java While Loop and the Condition inside the While loop will make sure that, the given number is greater than 0 (Means Positive integer)

From the above Java screenshot, User Entered value: Number= 9875 and Reverse= 0

Java Reverse a Number First Iteration

Reminder = Number % 10
Reminder = 9875 % 10 = 5

Reverse= Reverse* 10 + Reminder
Reverse= 0 * 10 + 5
Reverse= 5

Number= Number/ 10
Number= 9875 /10
Number= 987

Second Iteration: From the first Iteration, the values of both Number and Reverse has changed as: Number= 987 and Reverse= 5

Reminder = 987 % 10 = 7

Reverse= 5 * 10 + 7 = 50 + 7
Reverse= 57

Number= 987 /10 = 98

Third Iteration: From the Second Java reverse a number Iteration, Number= 98 and Reverse= 57

Reminder = 98 % 10 = 8

Reverse= 57 * 10 + 8 = 570 + 8
Reverse= 578

Number = 98 / 10 = 9

Fourth Iteration: From the 3rd Iteration, Number= 9 and Reverse= 578

Reminder = 9 %10 = 9

Reverse= 578 * 10 + 9 = 5780 + 9
Reverse= 5789

Number = 9 / 10 = 0

Here, For the next iteration, Number = 0. So, the while loop condition will fail

Program to Reverse a Number in Java Using For Loop

This program allows the user to enter any positive integer and then it will reverse a number using Java For Loop

import java.util.Scanner;

public class Example {	
	
	private static Scanner sc;	
	
	public static void main(String[] args) 
	{	
		int num, Reminder, rv;
		sc = new Scanner(System.in);
		
		System.out.println("\n Please Enter any Number you want : ");
		num = sc.nextInt();
		
		for(rv = 0; num > 0; num = num/10) {
			Reminder = num %10;
			rv = rv * 10 + Reminder;
		}
		System.out.format("\n Reverse of entered is = %d\n", rv);
	}
}

Java reverse number using for loop output

 Please Enter any Number you want : 
123487659

 Reverse of entered is = 956784321

We just replaced the While loop in the above example with the Java For Loop.

Program to Reverse a Number in Java using string buffer

This Java program allows the user to enter any positive integer and then it will reverse a number using built-in string buffer reverse function

package SimplerPrograms;

import java.util.Scanner;

public class ReverseNumberUsingSTring {
	private static Scanner sc;

	public static void main(String[] args) {
		int num;
		sc = new Scanner(System.in);
		
		System.out.println("\n Please Enter any Integer : ");
		num = sc.nextInt();
		
		String str = Integer.toString(num);
		String rev = new StringBuffer(str).reverse().toString();
		System.out.format("\n Result = %s", rev);
	}
}
 Please Enter any Integer : 
6597213

 Result = 3127956

The following statement will convert the integer value to string value and also assign the converted value to Str variable.

String str = Integer.toString(num);

Next, we used the String Buffer reverse function to reverse the string.

String rev = new StringBuffer(str).reverse().toString();

lastly, we are displaying the reversed string value

System.out.format("\n Result = %s", rev);

Program to Reverse a Number in Java using Functions

In this Java program we are using the same steps that we followed in our first example but we separated the logic and placed in the separate method.

import java.util.Scanner;

public class Example {	
	
	private static Scanner sc;	
	
	private static int rv = 0;
	
	public static void main(String[] args) 
	{
		int num;
		sc = new Scanner(System.in);
		
		System.out.println("\n Please Enter any Number you want  : ");
		num = sc.nextInt();
		
		rv = rvnum(num);
		System.out.format("\n Reverse of entered is = %d\n", rv);
	}
	
	public static int rvnum(int num) {
		int Reminder;
		while(num > 0) {
			Reminder = num %10;
			rv = rv * 10 + Reminder;
			num = num /10;
		}
		return rv;
	}
}
 Please Enter any Number you want : 
69875

 Reverse of entered is = 57896

If you observe the following statement, we are calling the rvnum(num) method and assigning the return value to integer variable Reverse.

rv = rvnum(num);

When the compiler reaches to rvnum(int num) line in main() program then the compiler will immediately jump to below function:

public static int rvnum(int num) {

Program to Reverse a Number in Java using Recursion

This Java program allows the user to enter any positive integer and then it will reverse the given number using Java Recursion concept. In this example, we are dividing the code using the Object Oriented Programming. To do this, First we will create a class which holds a method to reverse an integer recursively.

package SimplerPrograms;

public class ReverseNumber {
	int Reverse = 0, Reminder;
	public int NumberReverse(int Number) {
		if(Number > 0) {
			Reminder = Number %10; 
			Reverse = Reverse * 10+ Reminder;
			NumberReverse(Number /10);
		}
		return Reverse;
	}

}

Within the Main program, we will create an instance of the above specified class and call the methods

package SimplerPrograms;

import java.util.Scanner;

public class RevNumbUsingClass {
	private static Scanner sc;

	public static void main(String[] args) {
		int Number, Rev = 0;
		sc = new Scanner(System.in);
		
		System.out.println("\n Please Enter any Number you want : ");
		Number = sc.nextInt();
		
		ReverseNumber rn = new ReverseNumber();
		Rev = rn.NumberReverse(Number);
		System.out.format("\n Reverse of entered number is = %d\n", Rev);
	}
}

Java reverse number using Oops output

 Please Enter any Number you want : 
123456789

 Reverse of entered number is = 987654321

ReverseNumber Class Analysis:

First we declared an integer function NumberReverse with one argument. Within the function, we used the If statement to check whether the given number is greater than Zero or Not and if it is True then statements inside the If block will be executed. We already explained the Logic in above example.

NOTE: NumberReverse (Number / 10) statement will help to call the function Recursively with updated value. If you miss this statement then, after completing the first line it will terminate.

Main Class Analysis: First we created an instance / created an Object of the ReverseNumber Class

ReverseNumber rn = new ReverseNumber();

Next, we are calling the NumberReverse method.

Reverse = rn.NumberReverse(Number);

Lastly, System.out.println statement will print the output .