Saturday, May 12, 2012

Reverse Input Integer

   It would be better for us newbies in JAVA programming to have codes and run it in your own. Even in a longest or in the simplest way you can, as long as the computer can understand. In every program in JAVA or even in other languages, there are lot of ways you may use to make the program working.
   Example program written below will reverse an input user' s number(integer). For testing, if the user input the number 123, the program will then output 321.
 

import javax.swing.JOptionPane;

//Reverse.java
public class Reverse {

    public static void main(String[] args) {
        int number,remainder;
        String s,baligtad="";
     
        s=JOptionPane.showInputDialog("Maglagay ng number: ");
        //convert string (s) to integer (number)
        number=Integer.parseInt(s);
     
        //use "num" for the original value
        int num=number;
     
        do{
            remainder=number%10;
            number/=10;
            baligtad+=remainder;
                }while(number>0);
       
       //If you use "number" isntead "num", "0" will be the output output and not the number input
        JOptionPane.showMessageDialog(null,"number:   "+num+"\nreversed: "+baligtad);
    }
}
OUTPUT:



No comments:

Post a Comment