Java: Prime Number Program Not Working -
why int j
value 2? doesn't (int)realnum
mean must natural number?
scanner basicnum = new scanner(system. in ); string insertnum = joptionpane.showinputdialog(null, "insert number\n"); int realnum = integer.parseint(insertnum); int j = realnum = 1; if (realnum < 10000) { while ((realnum / j == (int) realnum)) { j++; } system.out.println(j); if (j > 2) { joptionpane.showmessagedialog(null, "it not prime!!"); } if (j < 2) { joptionpane.showmessagedialog(null, "it prime!"); } } else { joptionpane.showmessagedialog(null, "too large number!"); }
in java 8, can this:
boolean isprime(final int n) { return intstream.range(2, n / 2+1).nonematch(i -> n % == 0); }
on earlier versions, 1 should same work,
string insertnum = joptionpane.showinputdialog(null, "insert number\n"); int realnum = integer.parseint(insertnum); boolean prime = true; (int = 2; <= realnum / 2; i++) { if (realnum % == 0) { joptionpane.showmessagedialog(null, "it not prime!!"); prime = false; break; } } if (prime) { joptionpane.showmessagedialog(null, "it prime!"); }
Comments
Post a Comment