Examples
- Hello World Program in Java (Print Hello World in Java)
- Java Program to Add Two Numbers (Java Sum / Addition)
- Find Greatest of Three Numbers in Java (Largest Number Program)
- Prime Number Program in Java (Code to Check Prime or Not)
- Java Program for Fibonacci Series (Using for, while, recursion, scanner)
- Factorial Program in Java (Find Factorial of a Number in Java)
- How to Find Sum of Digits of a Number in Java?
- How to Reverse a Number in Java? Program & Examples
- How to Swap Two Numbers in Java? Programs With/Without Third Variable
- Even Odd Program in Java (Program to Check Number is Even or Odd)
- Vowel and Consonant Program in Java
- Java Program for Quadratic Equation (Find Roots With 3 Ways)
- Find Frequency of Characters in a String in Java (4 Ways)
- Remove Space from String in Java (Remove Whitespace)
- String Null Check in Java (String is Empty or Null) - 5 Ways
- How to Print String in Java? 6 Methods
- How to Get ASCII Value of Char in Java? Find ASCII Value
How to Reverse a Number in Java? Program & Examples
Let’s learn how to find the reverse of a number in Java using different approaches and programming concepts!
Reversing a number involves arranging its digits in the opposite order, creating a new number with the positions of the original digits reversed. Understanding various techniques to find the reverse of a number in Java is fundamental for numerous applications, such as data manipulation and number-based problem-solving.
Here, we will dive into different approaches to calculate the reverse of a number using Java programming. Each method offers unique insights into handling digit manipulation and number reversal, enabling you to choose the most suitable approach for different scenarios and optimize your code for efficiency.
Concepts to Learn for this Program:
So, let’s get started and write a program to reverse a number in Java!
Java Program to Reverse a Number Using while loop
Here's a program to reverse integer in Java using while loop:
Code
import java.util.Scanner;
public class NumberReverser {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an integer: ");
int number = scanner.nextInt();
int reversedNumber = reverseNumber(number);
System.out.println("Reversed number: " + reversedNumber);
}
private static int reverseNumber(int n) {
int reversed = 0;
while (n != 0) {
int digit = n % 10; // Extract the last digit of the number
reversed = reversed * 10 + digit; // Append the digit to the reversed number
n /= 10; // Remove the last digit from the number
}
return reversed;
}
}
Output
Enter an integer: 308
Reversed number: 803
Explanation
In this program, we use the Scanner class to take an integer input from the user. The user is prompted to enter an integer, and the program calculates the reverse of the number using a while loop.
The reverseNumber method takes an integer n as input and initializes a variable reversed to store the reversed number. In the while loop, we extract the last digit of the number using the modulo operator %, and then we append the digit to the reversed number by multiplying it by 10 and adding the digit. This process continues until all the digits have been processed, and the reversed number is obtained.
Finally, the program displays the reversed number of the given input.
Reverse Number Program in Java Using Recursion
Here's a Java program to reverse a given number using recursion:
Code
import java.util.Scanner;
public class NumberReverser {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an integer: ");
int number = scanner.nextInt();
int reversedNumber = reverseNumber(number);
System.out.println("Reversed number: " + reversedNumber);
}
private static int reverseNumber(int n) {
if (n < 10) {
return n; // Base case: If n is a single-digit number, return itself
} else {
int lastDigit = n % 10;
int remainingNumber = n / 10;
return lastDigit * (int)Math.pow(10, countDigits(remainingNumber)) + reverseNumber(remainingNumber);
}
}
private static int countDigits(int n) {
return (int) Math.log10(n) + 1;
}
}
Output
Enter an integer: 1234
Reversed number: 4321
Explanation
In this program, we use recursion to reverse a number. The reverseNumber method takes an integer n as input.
The base case is when n is a single-digit number (i.e., less than 10). In the base case, the method returns n itself, as the reverse of a single-digit number is the number itself.
The recursive case is when n is greater than or equal to 10. In the recursive case, we calculate the last digit of n (obtained using % 10) and the remaining part of n (obtained using / 10). We then reverse the remaining part of n recursively and concatenate it with the last digit to form the reversed number.
To calculate the reverse of the remaining part of n, we recursively call the reverseNumber method. To concatenate the reversed remaining part with the last digit, we multiply the last digit by 10 raised to the power of the number of digits in the remaining part, which is obtained using the countDigits method.
This process continues until n becomes a single-digit number, and the reversed number is obtained. The program then displays the reversed number of the given input using recursion.
Reverse of a Number in Java Using for loop
Here's a Java program to reverse a given number using a for loop:
Code
mport java.util.Scanner;
public class NumberReverser {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an integer: ");
int number = scanner.nextInt();
int reversedNumber = reverseNumber(number);
System.out.println("Reversed number: " + reversedNumber);
}
private static int reverseNumber(int n) {
int reversed = 0;
while (n != 0) {
int digit = n % 10; // Extract the last digit of the number
reversed = reversed * 10 + digit; // Append the digit to the reversed number
n /= 10; // Remove the last digit from the number
}
return reversed;
}
}
Output
Enter an integer: 98765
Reversed number: 56789
Explanation
In this program, we use a for loop to reverse the given number. The reverseNumber method takes an integer n as input.
We initialize a variable reversed to store the reversed number. Inside the while loop, we extract the last digit of the number using the modulo operator %, and then we append the digit to the reversed number by multiplying it by 10 and adding the digit. This process continues until all the digits have been processed, and the reversed number is obtained.
Finally, the program displays the reversed number of the given input using the for loop.