Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2015
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. import static javax.swing.JOptionPane.*;
  2. public class oppg4145{
  3. public static void main(String[]args){
  4.  
  5. int tall = 1;
  6. while (tall != 0){
  7. try {
  8.  
  9. String lesTall = showInputDialog("Les inn tall(avslutt med 0)");
  10. tall = Integer.parseInt(lesTall);
  11. System.out.println(isPrime(tall) ? lesTall + " " + "er primtall" : lesTall + " " + "er ikke primtall");
  12.  
  13. }catch(NumberFormatException nfe){
  14. System.out.println("Vennligst les inn ett tall");
  15. }
  16.  
  17. }
  18. }
  19.  
  20. public static boolean isPrime(int n){
  21. if (n == 1)
  22. return false;
  23. if (n == 2)
  24. return true;
  25. if (n % 2 == 0)
  26. return false;
  27. for (int d = 3; d <= (int) Math.sqrt(n); d++){
  28. if (n % d == 0)
  29. return false;
  30. }
  31. return true;
  32. }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement