Sunday, September 4, 2016

Java Program on How to get the prime numbers between a given range.

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,

Bhanu Namikaze

Author & Editor

Bhanu Namikaze is an Ethical Hacker, Passionate Blogger, Web Developer, Student and Mechanical Engineer. He Enjoys writing articles, Blogging, Solving Errors, Surfing and Social Networking. Feel Free To Contact Me @ Bhanu.

0 comments:

Post a Comment

Popular Posts

Copyrights @ Interview Questions.Me - Blogger Templates Designed by Templateism - Distributed By Protemplateslab

  • (91) 5544 654942
  • Bhanu@HackingDream.net
  • Interview Questions

Copyrights @ Interview Questions.Me -A Site By Bhanu In Association With Hacking Dream