I am trying to run this code to print the sum of all the prime numbers less than 2 million. This loop is never ending. Can anyone tell me what is wrong with the code? It see
Here is a complete program of Prime using JOptionPane, i.e. Java GUI
import javax.swing.*;
public class ChkPrime {
public static void main(String[] args) throws NumberFormatException {
String str = JOptionPane.showInputDialog(null, "Enter any number: ","Input...", 3);
try {
int num = Integer.parseInt(str);
if (num == 1)
JOptionPane.showMessageDialog(null, "Your inputed no. " + num + " is not prime.","Error!", 0);
for(int i = 2; i <= Math.sqrt(num); i++) {
if(num % i == 0) {
JOptionPane.showMessageDialog(null, "Your inputed no. " + num + " is not prime.","Error!", 0);
System.exit(0);
}
}
JOptionPane.showMessageDialog(null, "Your inputed no. " + num + " is prime.","Yeh! Got it!", 1);
}
catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Please input numbers...","Error!", 0);
main(null);
}
}
}