Prime Number in Java

Table of Contents

Prime Number in Java

Compile and Run the following code with Command-Prompt Or download Notepad++

For Compilation javac

For Execution java Sample

import java.util.Scanner;
class Sample
  public static void main(String[] args)
    int a,count = 0;
    Scanner sc = new Scanner(;
    System.out.println("Enter the number: ");
    a = sc.nextInt();
    for(int i = 1;i <= a;i++)
      if(a%i == 0)
    if(count == 0)
      System.out.println("The given number is Prime number");
      System.out.println("The given nmber is not a Prime number");

Leave a Reply