
Create a complete Java program that allows the user to enter a positive integer n, and which then creates and populates an int array with the first n prime numbers. Your program should then display the contents of the array elements. You program should also ensure that the user enters a positive value for n, and should loop until they do so.

import java.util.*;
class FindPrime{
static boolean isPrime(long number) {
boolean isPrime = false;
int i = (int) Math.ceil(Math.sqrt(number));
while (i > 1) {
if ((number != i) && (number % i == 0)) {
isPrime = false;
break;
} else if (!isPrime)
isPrime = true;
--i;
}
return isPrime;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter N: ");
long num = input.nextLong();
for (int i = 1; i < num; i++) {
if (isPrime(i)) {
System.out.println(i);
}
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.