-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaArray02.txt
More file actions
28 lines (27 loc) · 1.16 KB
/
JavaArray02.txt
File metadata and controls
28 lines (27 loc) · 1.16 KB
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
/*Write a java program that takes 2 integer numbers as input and calculates how many prime numbers exist between them.*/
import java.util.Scanner;
public class JavaArray02{
public static void main(String[]args){
Scanner sc=new Scanner(System.in);
int n1= sc.nextInt();
int n2=sc.nextInt();
int count=0;
if(n1<n2){
for(int i=n1;i<=n2;i++){
int div=0;
for(int j=2;j<=i/2;j++){
if(i%j==0){
div++;}}
if(div==0){
count++;}}}
else{
for(int i=n2;i<=n1;i++){
int div=0;
for(int j=2;j<=i/2;j++){
if(i%j==0){
div++;}}
if(div==0){
count++;}}}
System.out.println("There are "+count+" prime numbers between "+ n1+" and "+n2);
}
}