Question

In: Computer Science

For a given positive integer n, output the first n primes. Example: n=3, output: 2,3,5; n=7,...

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

Solutions

Expert Solution

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


Related Solutions

For a given integer n > 1, list all primes not exceeding n. Example: n=10, output:...
For a given integer n > 1, list all primes not exceeding n. Example: n=10, output: 2,3,5,7 n=16, output: 2,3,5,7,11,13 In Java please
Prove or disprove that 3|(n 3 − n) for every positive integer n.
Prove or disprove that 3|(n 3 − n) for every positive integer n.
using the fact that : A positive integer n ≥ 3 is constructive if it is...
using the fact that : A positive integer n ≥ 3 is constructive if it is possible to construct a regular n-gon by straightedge and compass, it is possible to construct the angle 2π/n. And that if both angles α and β can be constructed by straightedge and compass then so are their sums and differences.The outside angle of a regular n-gon is 2π/n. 1. Suppose that n = p^(α1) ··· p^(αk) where p ,··· , pk are distinct odd...
Given a positive integer k and an array A[1..n] that contains the quiz scores of n...
Given a positive integer k and an array A[1..n] that contains the quiz scores of n students in ascending order, design a divide and conquer algorithm to efficiently count the number of students that have quiz scores in (100(i − 1)/k, 100i/k] for integers 1 ≤ i ≤ k. Let group i be the set of students with quiz scores in (100(i − 1)/k, 100i/k] for integers 1 ≤ i ≤ k. The counting result should be stored in G[1..k],...
Prove: There are infinitely many primes of the form 6n − 1 (n is an integer).
Prove: There are infinitely many primes of the form 6n − 1 (n is an integer).
Use induction to prove that for any positive integer n, 8^n - 3^n is a multiple...
Use induction to prove that for any positive integer n, 8^n - 3^n is a multiple of 5.
HOW TO ANSWER IN PYTHON : PROBLEM: Given a positive integer (call it ​N), a position​...
HOW TO ANSWER IN PYTHON : PROBLEM: Given a positive integer (call it ​N), a position​ ​in that integer (call it ​P), and a transition integer (call it ​D). Transform ​N as follows: If the ​Pth​ digit of ​N from the right is from​ ​0 to 4, add ​D to it. Replace the ​Pth​ digit by the units digit of the sum. Then, replace all digits to the right of the ​Pth​ digit by 0. If the ​Pth​ digit of...
Given a list of positive integers c[0...n − 1], and a positive integer v, decides whether...
Given a list of positive integers c[0...n − 1], and a positive integer v, decides whether we can use numbers from c[0...n − 1] to make a sum of v, allowing any particular number in the list to be used multiple times. Or, mathematically, this means that there exists non-negative integer coefficients, x0, x1, ..., xn−1, such that v = x0c[0] + x1c[1] + ...xn−1c[n − 1]. For example, given c[0...3] = {2, 4, 6, 10}, and v = 17,...
1. Write a Java program that asks the user to input a positive integer n first,...
1. Write a Java program that asks the user to input a positive integer n first, then create an array of size n. Fill n random integers between 5 and 555, inclusively, into the created array. Output the sum of all the integers in the array and the average of all the integers in the array. 2 .Find the output of the following Java program and explain your answer
Let n be a positive integer. Prove that if n is composite, then n has a...
Let n be a positive integer. Prove that if n is composite, then n has a prime factor less than or equal to sqrt(n) . (Hint: first show that n has a factor less than or equal to sqrt(n) )
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT