Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

SOLVE THIS USING import java.util.Scanner; SOLVE THIS USING import java.util.Scanner; ONLY HINT : for loops / while / do while can be helpful to use

SOLVE THIS USING import java.util.Scanner;

image text in transcribed

SOLVE THIS USING import java.util.Scanner; ONLY

HINT : for loops / while / do while can be helpful to use

Problem 2: (4 marks) Write a Java program that prints the first n prime numbers, where nis a positive integer that is given as input (from the user). For example, if the user enters 8, the program prints out the first 8 primes as follows: The first 8 primes are: 2, 3, 5,7, 11,13, 17, 19. The program must print out the first n primes for every n given by the user. The program should terminate only when the user enters a number that is not positive (the sentinel value). As an example, the following is the output of a sample run of the program: Enter a positive integer 5 The first 5 primes are 2 3, 5, 7, 11. Enter another positive integer: 3 The first 3 primes are 2, 3, 5. Enter another positive integer: 8 The first 8 primes are 2 3, 5, 7, 11, 13, 17, 19. Enter another positive integer: 15 The first 15 primes are: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47. Enter another positive integer: 20 The first 20 primes are: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59 61, 67, 71. Enter another positive integer 0 OK Bye Note: A primeisa positive int (greater than 11 that is divisible only by 1 and by itself. teger

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DB2 11 The Database For Big Data And Analytics

Authors: Cristian Molaro, Surekha Parekh, Terry Purcell, Julian Stuhler

1st Edition

ISBN: 1583473858, 978-1583473856

More Books

Students also viewed these Databases questions