Fill in the code for this program to calculate the first 100 prime numbers. Each block of comments should have a line of

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Fill in the code for this program to calculate the first 100 prime numbers. Each block of comments should have a line of

Post by answerhappygod »

Fill in the code for this program to calculate the first 100prime numbers. Each block of comments should have a line of codeperforming that action described.(Each line of code is 2points)
//Declare class called primeNumbers
//Declare array of 100 ints called primes
//Begin main method
//Start by assigning 2 to the first spot in the array
//Create loop to initialize the rest of the array
//Call nextPrime method to fill the next array spot (to speed upthe process, call nextPrime method with the previous index
//calculated and the current value of i)
//End of loop and main method (just print the value at atprimes[99] so we can check it against the expected value
//Begin nextPrime method that takes in the int 'p' (previousindex) and returns an int 'n'(next prime)
//initialize n to p+1
//initialize counter to i-1
//Start a while loop to check numbers until we reach the nextprime
//Check whether number is prime (Note: Since we have allprevious primes in the array, we only need to check againstthem)
//if n is evenly divisible by the current prime being checked,we need to increment n and reset the counter back to i-1
//Check if n is evenly divisible
//Increment n
//reset counter
//else we decrease counter by 1
//if counter gets below 0, we have found our prime, and the loopcan end, returning p
in java
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply