Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Question: Write a C program that produces prime pairs, checks for a prime number, or prints the number of primes up to agiven value based

Question:

Write a C program that produces prime pairs, checks for a prime number, or prints the number of primes up to agiven value based on an initial code entered by the user. If the code is 1 (one), user is prompted to enter a posi-tive integer > 5 and < 10000, then the program will print a table of prime pairs according to the format shownon the sample interaction. If the code is 2, user is asked to enter a positive integer less than 2,000,000, the pro-gram will then check to see if the user input is a prime number or not and print the appropriate message. If thecode is 3, user is asked to input a positive integer less than 1,000,000, the program will then return the exactand approximate number of primes up to the user input. If the code is 0 (zero), program terminates. Any othercode is considered invalid. For codes 1, 2, and 3 the program should check that the user input is in the requiredrange. A sample interaction with the program is shown on a separate page.

Use the following formula for approximating the number of primes up to a given value.p(n)=n /(log n1)

Things to Remeber ( Please attempt to follow the outline the teacher has provided for us)

define TRUE 1 define FALSE 0 /* function main */ declare and initialize variables print the start message get the user code while (code != 0) if (code == 1) prompt the user get the input check for valid input assuming it is an integer set-up a while loop for checking print the table header set-up a for loop from 3 to <= user input if ( isprime(counter) && isprime(counter + 2) ) print the results else if (code == 2) prompt the user get the input check for valid input assuming it is an integer set-up a while loop for checking if (isprime(input) == TRUE) print "it is a prime" else print "it is not a prime" else if (code == 3) prompt the user get the input check for valid input assuming it is an integer set-up a while loop for checking call the two functions (for the exact & approximate) number_of_primes with the user input as argument print the result else print "Wrong code" print the start message get the user code end while print termination message return EXIT_SUCCESS signal to the operation system (OS) /* end of function main */ /* function isprime */ /* function isprime gets an integer and returns TRUE or FALSE */ // declare a counter for the "for" loop set up a "for" loop if (user input % counter == 0) return FALSE end for loop return TRUE /* end function isprime */ /* function number_of_primes */ /* function number_of_primes gets an integer and returns an integer */ use the following formula and cast it to an integer when returning the result. number of primes up to n is: n / (log n - 1) /* end function number_of_primes */ 

Sample Output

To generate prime pairs < 10000, enter 1 To check for a prime, enter 2 To determine the number of primes up to a given value, enter 3 To terminate the program, enter 0: 5 Wrong input To generate prime pairs < 10000, enter 1 To check for a prime, enter 2 To determine the number of primes up to a given value, enter 3 To terminate the program, enter 0: 1 Enter a positive integer > 5 and < 10000: 2 Wrong input Enter a positive integer > 5 and < 10000: 200 The prime pairs up to 200 are: 3 , 5 5 , 7 11 , 13 17 , 19 29 , 31 41 , 43 59 , 61 71 , 73 101 , 103 107 , 109 137 , 139 149 , 151 179 , 181 191 , 193 197 , 199 To generate prime pairs < 10000, enter 1 To check for a prime, enter 2 To determine the number of primes up to a given value, enter 3 To terminate the program, enter 0: 2 Enter a positive integer > 5 and < 2,000,000: 991 991 is a prime. To generate prime pairs < 10000, enter 1 To check for a prime, enter 2 To determine the number of primes up to a given value, enter 3 To terminate the program, enter 0: 2 Enter a positive integer > 5 and < 2,000,000: 9991 9991 is not a prime. To generate prime pairs < 10000, enter 1 To check for a prime, enter 2 To determine the number of primes up to a given value, enter 3 To terminate the program, enter 0: 3 Enter a positive integer > 5 and < 2,000,000: 3000000 Wrong input Enter a positive integer > 5 and < 2,000,000: 100 Exact number of primes up to 100 is: 25 Approximate number of primes up to 100 is: 27 To generate prime pairs < 10000, enter 1 To check for a prime, enter 2 To determine the number of primes up to a given value, enter 3 To terminate the program, enter 0: 3 Enter a positive integer > 5 and < 2,000,000: 1000 Exact number of primes up to 1000 is: 168 Approximate number of primes up to 1000 is: 169 To generate prime pairs < 10000, enter 1 To check for a prime, enter 2 To determine the number of primes up to a given value, enter 3 To terminate the program, enter 0: 0 *** Program Terminated *** 

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_2

Step: 3

blur-text-image_3

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

More Books

Students also viewed these Databases questions

Question

5-3 What are the current trends in computer hardware platforms?

Answered: 1 week ago