Add OOP/Java/Lab/Week4/PrimeArray.java
This commit is contained in:
parent
48775a2c5e
commit
829ba30125
34
OOP/Java/Lab/Week4/PrimeArray.java
Normal file
34
OOP/Java/Lab/Week4/PrimeArray.java
Normal file
@ -0,0 +1,34 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
class PrimeArray {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.print("Enter the size of the array: ");
|
||||
int size = sc.nextInt();
|
||||
|
||||
int[] arr = new int[size];
|
||||
System.out.println("Enter " + size + " integers:");
|
||||
for (int i = 0; i < size; i++) {
|
||||
arr[i] = sc.nextInt();
|
||||
}
|
||||
|
||||
System.out.print("Prime numbers in the array: ");
|
||||
for (int num : arr) {
|
||||
if (num > 1) {
|
||||
boolean isPrime = true;
|
||||
for (int j = 2; j <= Math.sqrt(num); j++) {
|
||||
if (num % j == 0) {
|
||||
isPrime = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (isPrime) {
|
||||
System.out.print(num + " ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sc.close();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user