简体   繁体   中英

how to do a program that determines if a number is prime or composite

Im a beginner at using Java. For some reason, I cant get my program to properly determine if the number I type in is a prime or a composite number. Can someone show me what i'm doing wrong?

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    boolean prime = true;

    System.out.println("Please enter a number");
    int num1 = in .nextInt();
    int numval = 1;
    for (int i = 2; i < num1; i = i + 1) {
        numval += 1;
        int test = num1;
    }
    if (num1 != 0) {
        System.out.println("This is a prime number");
        prime = false;

    }
    if (prime == true) {
        System.out.println("This is not a prime number");

    }
}

I'm not sure what you're trying to do with the test variable but try something like this which is more cleaner.

boolean prime = true;
int num1 = in .nextInt();
// We know that composite numbers start from 2 and that 1 is neither prime nor composite.
if (num1 < 2) {
   throw new IllegalArgumentException("Number has to greater than 1");
}
for (int i = 2; i < num1; i++) {
    if (num % i == 0) {
        prime = false;
        break;
    }
}
if (prime) {
    System.out.print("Number is a prime");
} else {
    System.out.print("Number is composite");
}

Prime number is that number whose has only two factors are 1 and itself. So you have to check that is it have any factor that is greater than 1 and less than N. For this reason you have to write a loop from 2 to N-1 to check whether it is possible to divide by any number from 2 to N-1 .

If it is possible to get any factor from the loop, then make variable prime as false and break from the loop because if we get one factor from the loop then it is not necessary to check the other factors.

And if we don't get any factor from the loop then the value of prime will be true as we initialized before.

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    boolean prime = true;

    System.out.println("Please enter a number");
    int num1 = in .nextInt();

    if(num1<2)
    {
        System.out.println("Number should be greater than 1");
        return;
    }

    for (int i = 2; i < num1; i = i + 1) {
        if (num1 % i == 0) {
            prime = false;
            break;
        }
    }
    if (prime == true) {
        System.out.println("This is a prime number");

    }
    if (prime == false) {
        System.out.println("This is not a prime number");

    }
}
  boolean prime = true;
    for (int i = 2; i * i <= num1; i = i + 1) {
      if (num1 % i == 0) {
        prime= false;
        break;
      }
    }
    if (prime && num1>1) {
      System.out.println("This is a prime number");
    }else {
      System.out.println("This is not a prime number");
    }

I know two ways: Let's keep in mind 'number' is used.

1: ((number % 2) == 0)
2: ((number & 1) == 0)

I believe that'll return true for any prime and false otherwise.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM