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.
Given any positive integer n, the hailstone sequence starting at n is obtained as follows. You...
Given any positive integer n, the hailstone sequence starting at n is obtained as follows. You write a sequence of numbers, one after another. Start by writing n. If n is even, then the next number is n/2. If n is odd, then the next number is 3n + 1. Continue in this way until you write the number 1. For example, if you start at 7, then the next number is 22 (3 × 7 + 1). The next...
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 positive integer n, write a recursive algorithm that returns the number of the digits...
Given a positive integer n, write a recursive algorithm that returns the number of the digits in n. For example, if the given number n is 12345, the algorithm should return 5. What is the time complexity of your algorithm? use java
Given a positive integer n, write a recursive algorithm that returns the number of the digits...
Given a positive integer n, write a recursive algorithm that returns the number of the digits in n. For example, if the given number n is 12345, the algorithm should return 5. What is the time complexity of your algorithm?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT