- Version
- Download 23
- File Size 3.36 KB
- File Count 1
- Create Date May 10, 2016
- Last Updated May 10, 2016
Divisibility Checker in Java
Divisibility Checker in Java
A sample program in java that will check if a certain number is divisible with a certain number. If the result returns to be a whole number then that number is divisible to the number also set by the user. The program is in a gui format that uses the swing class (joptionpane dialog boxes for input and output).
Source code:
import javax.swing.*;
public class DivisibilityCheckerGUI
{
public static void main(String args[]) {
String input1, input2;
int integer, divisibleBy ;
String cont="n";
do
{
input1 = JOptionPane.showInputDialog(null,"Enter a positive number: ");
integer=Integer.parseInt(input1);
input2 = JOptionPane.showInputDialog(null,"Is Divisible by: ");
divisibleBy=Integer.parseInt(input2);
if (integer % divisibleBy == 0 ){
JOptionPane.showMessageDialog(null,integer + " is divisible by " + divisibleBy );
} else {
JOptionPane.showMessageDialog(null,integer + " is not divisible by " + divisibleBy );
}
cont = JOptionPane.showInputDialog(null,"Try Again? (press y or n) ");
}while (cont.matches("y"));
JOptionPane.showMessageDialog(null,"Program closing" );
}
}