Reverse a Number in Java GUI

Submitted by: 
Language: 
Visitors have accessed this post 1633 times.

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.

  1. import javax.swing.*;

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.

  1. int n,remainder;
  2. int result = 0;
  3. String numInput;
  4.  
  5. numInput = JOptionPane.showInputDialog(null, "Enter a number:");
  6. n = Integer.parseInt(numInput);

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.

  1. while(n>0){
  2. remainder = n%10;
  3. result = result * 10 + remainder;
  4. n = n/10;
  5. }

5. Lastly, display the output of the program using JOptionPane.showMessageDialog.

  1. JOptionPane.showMessageDialog(null, "Reverse number is: " +result);


Output:

Reverse
Here's the full code of this tutorial:

  1. import javax.swing.*;
  2.  
  3. public class reverse {
  4.  
  5.  
  6. public static void main(String[] args) {
  7.  
  8.  
  9. int n,remainder;
  10. int result = 0;
  11. String numInput;
  12.  
  13. numInput = JOptionPane.showInputDialog(null, "Enter a number:");
  14. n = Integer.parseInt(numInput);
  15.  
  16.  
  17. while(n>0){
  18. remainder = n%10;
  19. result = result * 10 + remainder;
  20. n = n/10;
  21. }
  22.  
  23. JOptionPane.showMessageDialog(null, "Reverse number is: " +result);
  24. }
  25.  
  26. }

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


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.