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: 70% (10 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 = .
-
As you begin to understand organizational behavior, why do you think it has become a popular field of interest?. kop5
-
List and briefly explain the important features of a bond contract.
-
Venzuela Companys net income for 2008 is $50,000. The only potentially dilutive securities outstanding were 1,000 options issued during 2007, each exercisable for one share at $6. None has been...
-
Question 1 3 A manufacturing company is trying to determine the cost of materials used to produce an A . If Ds cost $ 3 and Fs cost $ 5 , how much does it cost to produce an A ( in D and F materials...
-
Your brother has just started a new job as the Controller of an IESBA restricted audit client. You do not serve on the audit engagement. What steps must you take to ensure your independence is not...
-
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...
-
On January 1, Espinoza Moving and Storage leased a truck for a four-year period, at which time possession of the truck will revert back to the lessor. Annual lease payments are $10,000 due on...
-
Subway sales have been declining since 2014. In the US, Subway has closed a number of stores due to over-expansion, outdated operations, and uninspiring menus. In Canada, Subway took a different...
-
Harvey Auto Parts purchased a new crane on September 1 for $35,000, paying $10,000 cash and signing a 7%, 12-month note for the remaining balance, interest to be paid at maturity. The crane is...
-
e4(k+1) Find the sum of the series. k = 1 8
-
Carla Vista Corp. sponsors a defined benefit pension plan for its employees. On January 1, 2025, the following balances relate to this plan Plan assets $489,900 Projected benefit obligation 616,700...
-
Question 2 of 8 Shirts were purchased for $12.50 each and were marked up by $18.75. During Christmas, they were discounted by $6.85 per shirt. a. What was the rate of markdown? % Round to two decimal...
-
Bif was in Gails office waiting to go to lunch with her. Gail owned a business in competition with Bifs business. When Gail excused herself to go to the restroom, Bif looked at her computer screen...
-
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.
-
Restate the following one-, three-, and six-month outright forward European term bid-ask quotes in forward points. Spot 1.3515 1.3532 One-Month 1.3528 1.3550 Three-Month 1.3544 1.3571...
-
The Storm Soccer Team sells season tickets and collects the cash in January at the beginning of the season. The team collected $57,750 for season tickets. The soccer season starts in February and the...
-
A company purchased $3,400 of merchandise on July 5 with terms 3/10, n/30. On July 7, it returned $600 worth of merchandise. On July 8, it paid the full amount due. The amount of the cash paid on...
Study smarter with the SolutionInn App