dimanche 14 janvier 2018

Trouble with creating a program that tells whether or not a number is prime

I cannot figure why this program that I came up won't work. I am supposed to make a program that takes uses inputted integer and finds out whether or not the number is prime. Right now the program just says every number is prime. I just started with java so I feel like I am just not seeing something obvious. Any help would be great, thanks guys.

public class assignment1 {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    System.out.println("Insert Number:");

    Scanner scan = new Scanner(System.in);

    int variable1 = scan.nextInt();

    boolean is_prime = true;

    for(int x = 2; x < (variable1 - 1); x++){
        if (variable1 % x == 0){
            is_prime = false;
        }
    }

    if (is_prime = true){
            System.out.println("Prime");
    }else if (is_prime = false){
            System.out.println("Not Prime");
    }





}

}

Aucun commentaire:

Enregistrer un commentaire