In this section, we will explore different methods to reverse a number in Java using a while loop, for loop, and recursion.
To reverse a number, you can follow the steps outlined below:
- Initialize a variable called “reverse” to 0.
- Use the modulo (%) operator to find the remainder of the given number when divided by 10.
- Multiply the variable “reverse” by 10 and add the remainder obtained in the previous step.
- Divide the number by 10 to remove the last digit.
- Repeat steps 2-4 until the number becomes 0.
- The final value of the “reverse” variable will be the reversed number.
There are multiple approaches to reverse a number in Java:
- Reverse a number using a while loop
- Reverse a number using for loop
- Reverse a number using recursion
Example:
To reverse the number 1234, we can use the following approach:
Initialize three variables number (the number to be reversed), the remainder (which will store the remainder), and reverse (which will store the reversed number). Set all three variables to 0.
Iteration 1:
number = 1234 remainder = 1234 % 10 = 4 reverse = 0 * 10 + 4 = 0 + 4 = 4 number = 1234 / 10 = 123
So the value of the number and reverse variable is 123 and 4, respectively.
Iteration 2:
number = 123 remainder = 123 % 10 = 3 reverse = 4 * 10 + 3 = 40 + 3 = 43 number = 123 / 10 = 12
So the value of the number and reverse variable is 12 and 34, respectively.
Iteration 3:
number = 12 remainder = 12 % 10 = 2 reverse = 43 * 10 + 2 = 430 + 2 = 432 number = 12 / 10 = 1
So the value of the number and reverse variable is 1 and 432, respectively.
Iteration 4:
number = 1 remainder = 1 % 10 = 1 reverse = 432 * 10 + 1 = 4320 + 1 = 4321 number = 1 / 10 = 0
In the above variable number becomes 0. Hence, we get the reverse number 4321.
Reverse a number using a while loop
ReverseNumberExample.java
public class ReverseNumberExample { public static void main(String[] args) { int number = 987654, reverse = 0; while(number != 0) { int remainder = number % 10; reverse = reverse * 10 + remainder; number = number/10; } System.out.println("The reverse of the given number is: " + reverse); } }
Output
The reverse of the given number is: 456789
Reverse a number using for loop
In the following program, a for loop has been used to reverse a number by removing its digits one by one until the number becomes zero.
ReverseNumberExample1.java
public class ReverseNumberExample1 { public static void main(String[] args) { int number = 123456, reverse = 0; //we have not mentioned the initialization part of the for loop for( ;number != 0; number=number/10) { int remainder = number % 10; reverse = reverse * 10 + remainder; } System.out.println("The reverse of the given number is: " + reverse); } }
Output
The reverse of the given number is: 654321
For the above program, we can also write the for loop like this:
for(;number != 0;) { int remainder = number % 10; reverse = reverse * 10 + remainder; number=number/10; }
Reverse a number using recursion
ReverseNumberExample2.java
import java.util.Scanner; public class ReverseNumberExample2 { //method for reverse a number public static void reverseNumber(int number) { if (number < 10) { //prints the same number if the number is less than 10 System.out.println(number); return; } else { System.out.print(number % 10); reverseNumber(number/10); } } public static void main(String args[]) { System.out.print("Enter the number that you want to reverse: "); Scanner sc = new Scanner(System.in); int num = sc.nextInt(); System.out.print("The reverse of the given number is: "); //method calling reverseNumber(num); } }
Output 1:
Enter the number that you want to reverse: 9 The reverse of the given number is: 9
Output 2:
Enter the number that you want to reverse: 7654123 The reverse of the given number is: 3214567
The program provided is designed to reverse both positive and negative numbers. It follows the following steps:
- Accept an input number from the user.
- Check if the number is positive or negative.
- If the number is negative, convert it to a positive number by multiplying it by -1.
- Proceed with the reversal process by performing the same steps as in previous programs.
- Reverse the number by iteratively extracting the last digit and adding it to the reversed number.
- Check the sign of the original input number.
- If the original number was negative, multiply the reversed number by -1 to make it negative again.
ReverseNumberExample3.java
import java.util.*; public class ReverseNumberExample3 { public static void main(String args[]) { System.out.print("Enter the number that you want to reverse: "); Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int reverse_number = reverseNumber(n); System.out.println("The reverse of the given number is: "+reverse_number); } //method to reverse a number public static int reverseNumber(int number) { boolean isNoNegative = number < 0 ? true : false; if(isNoNegative) { number = number * -1; //makes the number positive if the given number is negative } int reverse = 0; int lastDigit = 0; while (number >= 1) { lastDigit = number % 10; // gives the last digit of the number reverse = reverse * 10 + lastDigit; number = number / 10; // removes the last digit of the number } //makes the number negative return isNoNegative == true? reverse*-1 : reverse; } }
Output 1:
Enter the number that you want to reverse: -98765 The reverse of the given number is: -56789
Output 2:
Enter the number that you want to reverse: 321987 The reverse of the given number is: 789123
To gain further understanding of reversing a number in Java, you can follow tutorials.freshersnow.com.