diff --git a/OOP/Java/Lab/Week4/PrimeArray.java b/OOP/Java/Lab/Week4/PrimeArray.java new file mode 100644 index 0000000..a931541 --- /dev/null +++ b/OOP/Java/Lab/Week4/PrimeArray.java @@ -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(); + } +}