Reverse a Number in Java GUI
Submitted by donbermoy on Saturday, May 10, 2014 - 15:25.
In this tutorial, i will going to teach you how to create a program in Java GUI that reverses an inputted number.
Now let's start this tutorial!
1. Open JCreator or NetBeans and make a java program with a file name of reverse.java.
2. Import javax.swing package. Hence we will use a GUI (Graphical User Interface) here like the inputting the number.
3. Initialize your variable in your Main, variable numInput as string and make it as an input and variable n, remainder, and result as int. n variable will be used for parsing the numInput variable to be an integer, remainder to get the remainder of the inputted number that is divided by 10, and the result variable will hold the reverse value of the inputted number.
4. Make a While loop statement that the number inputted will be more than 0, the formula of reversing a number will be executed. The n variable will be divided by 10 and the remainder will hold the value of it. Hence we used the '%' function known as the modulus operator. Then the result variable will hold the value of the product of 10 and the remainder variable.
5. Lastly, display the output of the program using JOptionPane.showMessageDialog.
Output:
Here's the full code of this tutorial:
Best Regards,
Engr. Lyndon R. Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]
Add and Follow me on Facebook: https://www.facebook.com/donzzsky
Visit and like my page on Facebook at: https://www.facebook.com/BermzISware
- import javax.swing.*;
- int n,remainder;
- int result = 0;
- String numInput;
- while(n>0){
- remainder = n%10;
- result = result * 10 + remainder;
- n = n/10;
- }
- import javax.swing.*;
- public class reverse {
- int n,remainder;
- int result = 0;
- String numInput;
- while(n>0){
- remainder = n%10;
- result = result * 10 + remainder;
- n = n/10;
- }
- }
- }
Add new comment
- 1250 views