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
|
package
javaTutorial;
import
java.util.ArrayList;
import
java.util.Scanner;
public
class GetPrimeNumbers{
public
static void main(String[] args){
Scanner
in = new Scanner(System.in);
System.out.println("Enter
a number from which you want prime number: ");
int
p1 = in.nextInt();
System.out.println("Enter
one more number till which you want prime number: ");
int
p2 = in.nextInt();
ArrayList<Integer>
prime = new ArrayList<Integer>();
int
i=2;
for(int
p=p1; p<=p2; p++){
i=2;
for(;
i<10; i++){
if(p%i==0
&& p!=i){
break;
}
}
if(i==10){
prime.add(p);
}
}
System.out.println("Prime
numbers between "+p1+" and "+p2+" are: ");
for(int
j=0; j<prime.size(); j++){
System.out.print(prime.get(j).toString()+",
");
}
}
}
|
Output
1
2
3
4
5
6
|
Enter
a number from which you want prime number:
10
Enter
one more number till which you want prime number:
30
Prime
numbers between 10 and 30 are:
11,
13, 17, 19, 23, 29,
|
0 comments:
Post a Comment