repl.it
@Murzikal/

Prime Checker in Java

Java

No description

fork
loading
Files
  • Main.java
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.util.Scanner;

class Main 
{
  public static void main(String[] args) 
  {
      System.out.println("Welcome to the Prime Number checker");
      
      boolean prime = true;
      boolean run = true;
      Scanner input = new Scanner(System.in);

      while (run)
      {
          System.out.println();
          System.out.println("Enter the POSITIVE INTEGER that you would like to check, enter 0 to exit");
          int num = input.nextInt();
          
          if (num == 0) { 
              run = false;
              continue;
          }
          if (num == 1) {
              System.out.println("1 is a special case, not a prime.");
              continue;
          }
          for (int x = num - 1; x > 1; x = x - 1){
              if (x == 0){
                  prime = false;
                  continue;
              }
              int div = num / x;
              int check = x * div;
              if (check == num){
                  prime = false;
              }
          }
          if (prime == true){
              System.out.println(num + " is a prime!");
          }
          else {
              System.out.println(num + " is not a prime :(.");
          }
          prime = true;
      } 
  }
}
?