Add OOP/Java/Lab/Week4/PrimeArray.java

This commit is contained in:
Aadit Agrawal 2024-08-31 01:44:31 +05:30
parent 48775a2c5e
commit 829ba30125

View 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();
}
}