-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUFFPrime.java
45 lines (38 loc) · 906 Bytes
/
UFFPrime.java
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/* Write a program to print first 'n' prime numbers, take the value of
'n' from user */
import java.util.Scanner;
class UFFPrime
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter value for n : ");
int n = sc.nextInt();
int count = 0;
int num = 2;
while (count < n)
{
if (isPrime(num))
{
System.out.print(num + " ");
count++;
}
num++;
}
}
public static boolean isPrime(int num)
{
if (num <= 1)
{
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++)
{
if (num % i == 0)
{
return false;
}
}
return true;
}
}