-
Notifications
You must be signed in to change notification settings - Fork 3
/
AlternatePrimeNumberExample2.java
53 lines (53 loc) · 1.12 KB
/
AlternatePrimeNumberExample2.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
46
47
48
49
50
51
52
53
// Printing alternate prime numbers
import java.util.*;
public class AlternatePrimeNumberExample2
{
public static void main(String args[])
{
int n;
int status = 1;
int num = 3;
//object of the ArrayList class
List<Integer> primelist=new ArrayList<Integer>();
//object of the Scanner class
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of n: ");
//reading the value of n from the user
n = scanner.nextInt();
if (n >= 1)
{
System.out.println("First "+n+" prime numbers are: ");
//2 is a known prime number
System.out.println(2);
//adding 2 in the ArrayList
primelist.add(2);
}
for(int i = 2 ; i <=n ; )
{
for(int j = 2 ; j <= Math.sqrt(num) ; j++)
{
if(num%j == 0)
{
status = 0;
break;
}
}
if(status != 0)
{
System.out.println(num);
//adding numbers to the ArrayList
primelist.add(num);
i++;
}
status = 1;
num++;
}
System.out.println("Alternate prime numbers are:");
//loop for printing the alternate prime numbers
for(int k=0;k<primelist.size();k++)
{
if((k%2)==0)
System.out.println(""+primelist.get(k));
}
}
}