0

Same thing happens when I type in number: 15,21,27,33 etc, basically every odd number divisible by 3.

Here's the code:

package com.company;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter Number");
        int n = scanner.nextInt();

        for (int i = 2; i < n/2; ++i) {
            if (n % i == 0) {
                System.out.println("Not Prime");
                break;
            }
            else{
                System.out.println("Prime");
                break;
            }
        }
    }
}
khelwood
  • 55,782
  • 14
  • 81
  • 108
  • 3
    You print `"Prime"` and break as soon as you find any number that is not a factor. You need to let your loop run until you rule out the rest of the possible factors. – khelwood Dec 10 '19 at 18:06
  • Please look into the Sieve of Eratosthenes: https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes – duffymo Dec 10 '19 at 18:27

0 Answers0