Program to check whether a number is prime number or Not in java

Oct 7, 2019

In this program ,we check this number is prime number or not in java programming language..

PrimeNumber.java

import java.util.Scanner;
 class  Prime Number
 {
 public static void main(String args[])
 {
 int n;
 int count=0;  //division counter
 Scanner sc=new Scanner(System.in);
 System.out.print("Enter a number");
 n=sc.nextInt();
 for(int i=1;i<=n;i++)
 {
 if(n%i==0)
 {
 count++;  //increment
 }
 }
 if(count==2)
 {
 System.out.println(n+"is a Prime number");
 }
 else
 {
 System.out.println(n+"is Not Prime number");
 }
 }
 }
 /*OUTPUT:
 (Run 1)
 Enter a number : 5
 5 is a prime number
 (Run 2)
 Enter a number : 8
 8 is not a Prime number
 */

Leave a Reply

Your email address will not be published. Required fields are marked *