Prime Number

import java.io.*;
public class Prime
{
public static void main(String[] args)throws IOException
{
int n,i,c=0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Number to Check :-");
n = Integer.parseInt(br.readLine());

for(i=1;i<=n;i++)
{
if(n%i==0)
c++; // counter for counting the factors
}

if(c==2) // for a number to be prime its factors should not be more than 2
{
System.out.println("The given number is Prime");
}
else
{
System.out.println("The given number is not Prime");
}
}
}

/*
Example :-
Enter Number to Check :-
INPUT :-
5
OUTPUT:-
The given number is Prime
*/ 

Comments

Popular Posts