-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPrintPrime.java
39 lines (35 loc) · 955 Bytes
/
PrintPrime.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
/*
Q: Write a java program to print all the prime numbers in a given interval.
*/
import java.util.Scanner;
public class PrintPrime {
// Method to print the prime number in the given range
static void printPrime(int start, int end) {
System.out.println("Prime numbers between " + start + " and " + end + " are:");
int count;
// Looping the number from start to end
for(int i = start; i <= end; i++) {
count = 0;
// Check if the number is prime and if it is prime then print
for (int j = 2; j <= i/2; j++) {
if(i % j == 0) {
count++;
break;
}
}
if(count == 0) {
System.out.print(i + " ");
}
}
}
// Main method
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the starting number: ");
int start = scan.nextInt();
System.out.println("Enter the ending number: ");
int end = scan.nextInt();
printPrime(start, end);
scan.close();
}
}