In this tutorial, you’ll learn how to reverse a number in Java. You’ll see how to do it with different techniques and find out which one is the most efficient for your needs.
Post Type: | Java Programs For Beginners |
Published On: | www.softwaretestingo.com |
Applicable For: | Freshers & Experience |
Get Updates: | Join Our Telegram Group |
Reverse Number In Java
We’ve talked about the different ways you can reverse a number in a Java program. The compiler is added to each and every program so it can be executed. Examples are given with accompanying sample outputs.
- Using While Loop
- Using For Loop
- Using Static Method
- Using Function
- Using Recursion
Reverse Number In Java Using While Loop
This method will allow us to use a while loop to break down the number input and rearrange it in reverse order. We’ll be using the modulo operator to extract digits from the number, and the divide operator to shorten it.
package com.softwaretestingo.interviewprograms; import java.util.Scanner; public class ReverseNumberEx1 { public static void main(String[] args) { int num=0; int reversenum =0; System.out.println("Input your number and press enter: "); //This statement will capture the user input Scanner in = new Scanner(System.in); //Captured input would be stored in number num num = in.nextInt(); //While Loop: Logic to find out the reverse number while( num != 0 ) { reversenum = reversenum * 10; reversenum = reversenum + num%10; num = num/10; } System.out.println("Reverse of input number is: "+reversenum); } }
Output:
Input your number and press enter: 152 Reverse of input number is: 251
Reverse Number In Java Using For Loop
In this method, we will use a for loop instead of a while loop.
package com.softwaretestingo.interviewprograms; import java.util.Scanner; public class ReverseNumberEx2 { public static void main(String[] args) { int num=0; int reversenum =0; System.out.println("Input your number and press enter: "); //This statement will capture the user input Scanner in = new Scanner(System.in); //Captured input would be stored in number num num = in.nextInt(); //While Loop: Logic to find out the reverse number //loop to find reverse number for( ;num != 0;) { reversenum = reversenum * 10; reversenum = reversenum + num%10; num = num/10; }; System.out.println("Reverse of input number is: "+reversenum); } }
Output:
Input your number and press enter: 159 Reverse of input number is: 951
Reverse Number In Java Using Recursion
This method divides a number by 10 and displays the remainder. It then calls itself, passing the quotient as a parameter. This process continues until the number is in single digits. The last digit (which is also the first digit of the original number) is displayed, and then the recursion ends.
package com.softwaretestingo.interviewprograms; import java.util.Scanner; public class ReverseNumberEx3 { public static void main(String[] args) { int num=0; int reversenum =0; System.out.println("Input your number and press enter: "); //This statement will capture the user input Scanner in = new Scanner(System.in); //Captured input would be stored in number num num = in.nextInt(); System.out.println ("Reversed Number: " + getReverse(num, reversenum)); } static int getReverse (int num, int rev) { if (num == 0) return rev; int rem = num % 10; rev = rev * 10 + rem; return getReverse (num / 10, rev); } }
Output:
Input your number and press enter: 123 Reversed Number: 321
You can do the same regression using the same recursive function with a single parameter like below
package com.softwaretestingo.interviewprograms; import java.util.Scanner; public class ReverseNumberEx4 { public static void main(String[] args) { int num=0; int reversenum =0; System.out.println("Input your number and press enter: "); //This statement will capture the user input Scanner in = new Scanner(System.in); //Captured input would be stored in number num num = in.nextInt(); getReverse(num); } static void getReverse (int num) { if (num == 0) return; int rem = num % 10; System.out.print (rem); getReverse (num / 10); } }
Output:
Input your number and press enter: 789 987
Conclusion:
In this blog post, we have tried to share various possible ways to Reverse Number In Java. If you found its helpful, please share it with your friends and colleagues. If you have any questions or feedback, drop a comment below.
Leave a Reply