-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFirstNPrime.java
More file actions
31 lines (29 loc) · 782 Bytes
/
FirstNPrime.java
File metadata and controls
31 lines (29 loc) · 782 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//Author : Deepansh Dubey.
//Date : 2/09/2021.
//Purpose : To print first n prime numbers.
import java.io.*;
class FirstNPrime{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
boolean isPrime;
System.out.println("Please enter the no.");
int p=Integer.parseInt(br.readLine());
for(int i=2;i<=p;i++)
{
isPrime=true;
for(int j=2;j<=Math.sqrt(i);j++)
{
if(i%j==0)
{
isPrime=false;
break;
}
}
if (isPrime)
{
System.out.print(i+", ");
}
}
}
}