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 Sample.java

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.in);
    System.out.println("Enter the number: ");
    a = sc.nextInt();
    for(int i = 1;i <= a;i++)
    {
      if(a%i == 0)
      {count++;}
    }
    if(count == 0)
      System.out.println("The given number is Prime number");
    else 
      System.out.println("The given nmber is not a Prime number");
  }
}
Previous Largest among all in Java
Next Swap Logic 1 in Java

Leave a Reply