Modify Listing 5.15 to display all the prime numbers between 2 and 1,000, inclusive. Display eight prime
Question:
Modify Listing 5.15 to display all the prime numbers between 2 and 1,000, inclusive. Display eight prime numbers per line. Numbers are separated by exactly one space.
Listing 5.15
Transcribed Image Text:
1 public class PrimeNumber { public static void main(String[] args) { final int NUMBER_OF_PRIMES = 50; // Number of primes to display final int NUMBER_OF_PRIMES_PER_LINE = 10; // Display 10 per line int count = 0; // Count the number of prime numbers int number = 2; // A number to be tested for primeness 2 4. 5 8 System.out.println("The first 50 prime numbers are \n"); // Repeatedly find prime numbers while (count < NUMBER_OF_PRIMES) { // Assume the number is prime boolean isPrime = true; // Is the current number prime? 10 11 12 13 count prime numbers 14 15 // Test whether number is prime for (int divisor = 2; divisor <= number / 2; divisor++) { if (number % divisor == 0) { // If true, number is not prime isPrime = false; // Set isPrime to false break; // Exit the for loop check primeness 16 17 18 19 20 exit loop 21 22 // Display the prime number and increase the count if (isPrime) { count++; // Increase the count 23 24 25 display if prime 26 27 if (count % NUMBER_OF_PRIMES_PER_LINE == 0) { // Display the number and advance to the new line System.out.println(number); 28 29 30 31 32 else System.out.print(number + "); 33 // Check if the next number is prime number++; 36 37 38 39 }
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 100% (6 reviews)
Program plan Take as input numbers from 2 and 1000 Calcula...View the full answer
Answered By
GERALD KAMAU
non-plagiarism work, timely work and A++ work
4.40+
6+ Reviews
11+ Question Solved
Related Book For
Introduction to Java Programming, Comprehensive Version
ISBN: 978-0133761313
10th Edition
Authors: Y. Daniel Liang
Question Posted:
Students also viewed these Computer science questions
-
Write a program that displays all the prime numbers less than 120 in decreasing order. Use the StackOfIntegers class to store the prime numbers (e.g., 2, 3, 5,... ) and retrieve and display them in...
-
Write a program that obtains the execution time for finding all the prime numbers less than 8,000,000, 10,000,000, 12,000,000, 14,000,000, 16,000,000, and 18,000,000 using the algorithms in Listings...
-
Modify Listing, TestWeightedGraph.java, to create a file for representing graph1. The file format is described in Exercise. Create the file from the array defined in lines 7?24 in Listing. The number...
-
In thinking about the appropriate balance between the market and the government in achieving sustainability, do you think the government needs to take a stronger role or would you favor reducing...
-
Prove the complement laws in Table 1 by showing that a) A = U. b) A = .
-
Draw gravitational field lines between 2 masses, electric field lines between a positive and a negative charge, electric field lines between 2 positive charges and magnetic field lines around a...
-
Consider the following cash flow profile and assume MARR is 10 percent/year. a. Determine the IRR(s) for this project. b. Is this project economically attractive? EOY 0 1 2 3 4 5 6 NCF -$101 $411...
-
Scott Mills was originally a producer of fabrics, but several years ago intense foreign competition led management to restructure the firm as a vertically integrated cotton garment manufacturer....
-
Consider the following ridiculously simple linear system: 10 (9)(2)-(0) a. What is the solution of this linear system? You don't have to show your work. b. Set up a Neumann iteration with initial...
-
Assume that the network in Figure 20.34 (previous problem) uses distancevector routing with the forwarding table as shown for each node. If each node periodically announces their vectors to the...
-
Write a program that lets the user enter the loan amount and loan period in number of years and displays the monthly and total payments for each interest rate starting from 5% to 8%, with an...
-
Use nested loops that display the following patterns in four separate programs: Pattern B 12 3 4 5 6 2 3 4 5 123 4 Pattern D 1 2 3 4 5 6 12 3 4 5 123 4 Pattern A Pattern C 2 1 3 2 1 4 3 2 1 5 4 3 2 1...
-
Sketch the graph of a function f that satisfies all the following conditions. (a) Its domain is [-2, 2]. (b) f(-2) = f(-1) = f(1) = f(2) = 1. (c) It is discontinuous at - 1 and 1. (d) It is right...
-
1. Identify and discuss three types of production as well as growth strategies of firms. 2. Describe how a firm in gold mining can grow and expand.
-
4. Your production function is Q = LK. The wage for L is w and the rental rate for K is r. You need to produce Q units of output. (a) What is your total cost equation? (b) What is your output...
-
Consider a firm in a market with rising production costs. Using a supply-demand graph and briefly explaining in words, explain why this firm would be less hurt if demand is inelastic than if demand...
-
What is a fiscal policy that the president and Congress might do to keep output stable, at least in the short run?
-
Scenario 11.1 Maui Macadamia Inc. has a monopoly in the macadamia nut industry. The demand curve, marginal revenue and marginal cost curve for macadamia nuts are given as follows: P = 360 - 4Q; MR =...
-
Your comparison of the gross margin percentage for Jones Drugs Ltd for the years 20X1 to 20X4 indicates a significant decline. This is shown by the following information: A discussion with Marilyn...
-
Wilsons Auto Repair ended 2011 with Accounts Receivable of $85,000 and a credit balance in Allowance for Uncollectible Accounts balance of $11,000. During 2012, Wilsons Auto Repair had the following...
-
Assuming a variable y has been assigned the value 6, the value of ! (y < 7) is ____________. a. 6 b. 7 c. True d. False
-
Write an application that asks a user to enter an integer. Display a statement that indicates whether the integer is even or odd. Save the file as EvenOdd.java.
-
Write an application that asks a user to enter three integers. Display them in ascending and descending order. Save the file as AscendingAndDescending.java.
-
List 3 different types of market segmentations. Let's hypothesize that you are the CEO of a hockey equipment company. Who would likely be your main market segment? Explain your answer. List 3...
-
what ways does the Multilevel Feedback Queue (MLFQ) scheduling algorithm address the complexities of modern computing environments by incorporating multiple priority levels and feedback mechanisms?
-
Part 1 : Use the table provided here to answer the questions below: 1) which attribute is the most important and what is the decision weight of each attribute 2) What is the market share between the...
Study smarter with the SolutionInn App