COME 2 CODE
Learn by Yourself

Program to find whether the given number is Prime

Hello friends, in this tutorial we are going to write a program to find whether the given number is prime or not. So, open your IDE and make a new Project. Now, write the following code in Main.java file.

Main.java :

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    int num, temp = 0;

    System.out.println("Enter the number : ");
    Scanner scanner = new Scanner(System.in);
    num = scanner.nextInt();

    for (int i = 2; i < num; i++) {
      if ((num % i) == 0) {
        temp = 1;
        break;
      }
    }

    if (temp == 1 || num == 0 || num == 1) {
      System.out.println("Number is not prime");
    } else if (temp == 0) {
      System.out.println("Number is prime");
    }
  }
}

Your output will be like this.

Output :

C:/Users/Username/Desktop>Enter the number :
107
Number is prime
QUICK LINKS :