In: Computer Science
For a given positive integer n, output the first n primes.
Example:
n=3, output: 2,3,5;
n=7, output: 2,3,5,7,11,13,17.
In Java please
import java.util.Scanner; public class PrintPrimeNumbers { public static boolean isPrime(int number) { for (int i = 2; i < number; i++) { if (number % i == 0) { // If true, number is not prime return false; // number is not a prime } } return number > 1; // number is prime } public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("How may primes do you want? "); int n = in.nextInt(), count = 0, num = 2; System.out.print("First " + n + " prime numbers are: "); while (count < n) { if (isPrime(num)) { count++; System.out.print(num + " "); } num++; } System.out.println(); } }