# Java Program to Reverse a Number

Reversing a number in Java means taking a number and producing a new number with its digits in the opposite order. For example, if the given number is “1234”, then the reverse number will be “4321”.

Problem Description

Write a Java Program to find the reverse of the number.

Problem Solution

1. Enter any integer number as an input.
2. After that, we use modulus and division operations respectively to generate output.

There are several ways to reverse a number in Java language. Let’s take a detailed look at all the approaches to reverse a number in Java.

Method 1: Reverse a Number in Java using While Loop

To reverse a number, we are using while loop and some arithmetic operations. The idea is to extract each digit of the number one by one, starting from the last digit, and concatenate them in reverse order.

Program/Source Code

Here is the source code of the Java Program to Reverse a Given Number using while loop. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

```/*
* Java program to reverse a number using While Loop
*/

import java.util.Scanner;
public class Reverse_Number
{
public static void main(String args[])
{
int m, n, sum = 0;
Scanner s = new Scanner(System.in);
System.out.print("Enter the number:");
m = s.nextInt();
while(m > 0)
{
n = m % 10;
sum = sum * 10 + n;
m = m / 10;
}
System.out.println("Reverse of a Number is "+sum);
}
}```
Program Explanation

1. The program prompts the user to input a number using the Scanner class.
2. Using a while loop, the program extracts each digit of the number starting from the last digit.
3. To reverse the order of the digits, the program concatenates them in reverse order using modulus and division operations.
4. print the given number and its reverse as output.

Time complexity: O(n)
The time complexity of this program is O(n), where n is the number of digits in the number.

Space complexity: O(1)
Because no extra space is used to store the digit, the space complexity of this program is O(1).

Note: Join free Sanfoundry classes at Telegram or Youtube
Runtime Test Cases

Testcase 1: In this case, we are entering the number “323441” as input.

```\$ javac Reverse_Number.java
\$ java Reverse_Number

Enter the number:
323441

Reverse of a Number is 144323```

Testcase 2: In this case, we are entering the number “42394” as input.

```\$ javac Reverse_Number.java
\$ java Reverse_Number

Enter the number
42394

Reverse of a Number is 49324```

Method 2: Reverse a Number in Java using For Loop

In this approach, we will use a for loop to reverse a number.

Program/Source Code

Here is the source code of the Java Program to Reverse a Given Number using for loop. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

```/*
* Java program to reverse a number using For Loop
*/

import java.util.Scanner;

public class ReverseNumber
{
public static void main(String[] args)
{
int m, n, reversed = 0;
Scanner s = new Scanner(System.in);
System.out.print("Enter the number: ");
m = s.nextInt();
for (; m != 0; m /= 10) {
n = m % 10;
reversed = reversed * 10 + n;
}
System.out.println("Reversed Number: " + reversed);
}
}```
Program Explanation

1. This is a Java program that uses the Scanner class to take input from the user and reverse a number entered by the user.
2. The program defines a class called ReverseNumber with a main method that takes an array of String arguments.
3. The main method declares three integer variables m, n, and reversed, and creates a new instance of the Scanner class to read input from the user.
4. The user is prompted to enter a number using the print method of the System class.
5. The nextInt method of the Scanner class is used to read the user’s input as an integer and assign it to the variable m.
6. A for loop is used to reverse the number entered by the user. The loop runs as long as m is not equal to 0.
7. In each iteration of the loop, the remainder of m divided by 10 is assigned to the variable n, and reversed is updated by multiplying it by 10 and adding n to it.
8. After the loop finishes, the program prints the reversed number using the println method of the System class.

Time complexity: O(log n)
The time complexity of the Java program that reverses a number using a for loop is O(log n), as it involves iterating through the digits of the number.

Space complexity: O(1)
The space complexity is O(1), as it uses a fixed amount of memory to store the variables.

Program Output:

In this case, we are entering the number “323441” as input.

```Enter the number:
323441

Reversed Number: 144323```

Method 3: Reverse a Number in Java using Recursion

In this approach, we will use a recursive function to calculate the reverse of the number.

Approach to Reverse a Number using Recursion in Java
1. Enter any integer number as an input.
2. After that we count the number of digits in the given input.
3. 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.

Program/Source Code

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.

```/*
* Java program to reverse a number using Recursion
*/

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));
}
}
}```
Program Explanation

1. The Reverse_Recursion class has a main method that reads the input and counts the number of digits.
2. An object is created to call the reverse method which takes two arguments: the number and its count.
3. If the count is 1, the method returns the number, otherwise it extracts the last digit using modulo and calls itself recursively to extract the remaining digits.
4. The digits are then added to the reversed number using the power function.
5. Finally, the reversed number is printed using the println method of the System class.

Time complexity: O(log n)
The time complexity of this program is O(log n), where n is the input number. This is because the number of recursive calls is proportional to the number of digits in the input number, which is logarithmic in base 10.

Space complexity: O(log n)
The space complexity of this program is also O(log n), because the recursive calls consume memory on the call stack, and the maximum depth of the call stack is proportional to the number of digits in the input number, which is logarithmic in base 10.

Program Output:

In this case, we are entering the number “467” as input to reverse the number.

```\$ javac Reverse_Recursion.java
\$ java Reverse_Recursion

Enter the number:467
Reverse:764```

To practice programs on every topic in Java, please visit “Programming Examples in Java”, “Data Structures in Java” and “Algorithms in Java”.

If you find any mistake above, kindly email to [email protected]