Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import static javax.swing.JOptionPane.*;
- public class oppg4145{
- public static void main(String[]args){
- int tall = 1;
- while (tall != 0){
- try {
- String lesTall = showInputDialog("Les inn tall(avslutt med 0)");
- tall = Integer.parseInt(lesTall);
- System.out.println(isPrime(tall) ? lesTall + " " + "er primtall" : lesTall + " " + "er ikke primtall");
- }catch(NumberFormatException nfe){
- System.out.println("Vennligst les inn ett tall");
- }
- }
- }
- public static boolean isPrime(int n){
- if (n == 1)
- return false;
- if (n == 2)
- return true;
- if (n % 2 == 0)
- return false;
- for (int d = 3; d <= (int) Math.sqrt(n); d++){
- if (n % d == 0)
- return false;
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement