Program #3: Five More Primes, Please Please write a program that will accept an
ID: 3750736 • Letter: P
Question
Program #3: Five More Primes, Please Please write a program that will accept an integer from the user, then will immediately print out the next five prime numbers equal to or greater than that integer. The program should then ask the user (with a "y/n" question) whether s/he would like to see another five integers. If so, it should print out the next five prime numbers in sequence, continuing to ask the user after each set whether more are desired. When the user indicates that no more prime numbers are desired, the program should end. You may use a function from a lecture example as part of this program if you would like. Doing so will likely make the program much easier to write. Hint: A do-while loop with another while loop nested inside of it is one possible way to write this progranm. Here is sample output for this program: Where would you like to begin searching for primes?100000 100003 is prime. 100019 is prime. 100043 is prime 100049 is prime. 100057 is prime. Would you like another five? (y/n)y 100069 is prime. 100103 is prime. 100109 is prime. 100129 is prime. 100151 is prime. Would you like another five? (y/n)y 100153 is prime. 100169 is prime 100183 is prime. 100189 is prime. 100193 is prime Would you like another five? (y/n)n Please turn in both your program listing (following all good programming practices we have discussed) and your output. Please clearly indicate them as "Program 3 Code" and "Program 3 Output.Explanation / Answer
Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
#include <stdio.h>
int isPrime(long n);
int main(){
long num;
char choice = 'y';
int count;
printf("Where would you like to begin searching for primes? ");
scanf("%ld", &num);
while(choice == 'y' || choice == 'Y'){
count = 1;
while(count <= 5){
if(isPrime(num)){
printf("%ld is prime. ", num);
count++;
}
num++;
}
printf("Would you like another five? (y/n): ");
getchar(); //get rid of new line
choice = getchar();
}
}
int isPrime(long n){
long i;
for(i = 2; i <= n/2; i++){
if(n % i == 0)
return 0;
}
return 1;
}
output
------
Where would you like to begin searching for primes? 100000
100003 is prime.
100019 is prime.
100043 is prime.
100049 is prime.
100057 is prime.
Would you like another five? (y/n): y
100069 is prime.
100103 is prime.
100109 is prime.
100129 is prime.
100151 is prime.
Would you like another five? (y/n): y
100153 is prime.
100169 is prime.
100183 is prime.
100189 is prime.
100193 is prime.
Would you like another five? (y/n): n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.