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 this program will reverse a number using Java While Loop

// Program to Reverse a Number in Java 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 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)

System.out.println("\n Please Enter any Number you want to Reverse : ");
Number = sc.nextInt();

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)

while(Number > 0) {
	Reminder = Number %10;
	Reverse = Reverse * 10 + Reminder;
	Number = Number /10;
}

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 = Number % 10
Reminder = 987 % 10 = 7

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

Number = Number / 10
Number = 987 /10
Number = 98

Third Iteration: From the Second Iteration, the values of both Number and Reverse changed as Number = 98 and Reverse = 57

Reminder = Number % 10
Reminder = 98 % 10 = 8

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

Number = Number /10
Number = 98 / 10
Number = 9

Fourth Iteration: From the 3rd Iteration, the values of both Number and Reverse has changed as Number = 9 and Reverse = 578

Reminder = Number % 10
Reminder = 9 %10 = 9

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

Number = Number /10 => 9 / 10
Number = 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 Java program allows the user to enter any positive integer and then, this program will reverse a number using Java For Loop

// Program to Reverse a Number in Java Using For Loop

package SimplerPrograms;

import java.util.Scanner;

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

Java reverse number using for loop output

 Please Enter any Number you want to Reverse : 
123487659

 Reverse of entered number 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 Reverse Function

This Java program allows the user to enter any positive integer and then, this program 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 number;
		sc = new Scanner(System.in);
		
		System.out.println("\n Please Enter any Number you want to Reverse : ");
		number = sc.nextInt();
		
		String str = Integer.toString(number);
		String reverse = new StringBuffer(str).reverse().toString();
		System.out.format("\n Reverse of entered number is = %s", reverse);
	}
}

Java number reverse output

 Please Enter any Number you want to Reverse : 
6597213

 Reverse of entered number is = 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(number);

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

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

lastly, we are displaying the reversed string value

System.out.format("\n Reverse of entered number is = %s", reverse);

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.

// Program to Reverse a Number in Java Using Functions
package SimplerPrograms;

import java.util.Scanner;

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

Java reverse number using functions output

 Please Enter any Number you want to Reverse : 
69875

 Reverse of entered number is = 57896

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

Reverse = ReverseNumber(Number);

When the compiler reaches to ReverseNumber (Number) line in main() program then the compiler will immediately jump to below function:

public static int ReverseNumber(int Number) {

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 ReverseNumberUsingClass {
	private static Scanner sc;

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

Java reverse number using Oops output

 Please Enter any Number you want to Reverse : 
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 .