Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Language: C++ Write a program that reads in a minimum integer and a maximum integer and displays all how many and all prime numbers between

Language: C++

Write a program that reads in a minimum integer and a maximum integer and displays all how many and all prime numbers between the minimum and maximum. The program prints error messages and prompts for the minimum and maximum values until it gets two values greater than or equal to 2 and such that the minimum is less than or equal to the maximum.

Sample Runs (Program Behavior)

Test 1 > run Enter minimum and maximum: 0 1 Error. Minimum and maximum must be at least 2. Enter minimum and maximum: 2 2 There is 1 prime number in this range. Primes: 2 Test 2 > run Enter minimum and maximum: -5 6 Error. Minimum and maximum must be at least 2. Enter minimum and maximum: 2 2 There is 1 prime number in this range. Primes: 2 Test 3 > run Enter minimum and maximum: 1 -3 Error. Minimum and maximum must be at least 2. Enter minimum and maximum: 2 2 There is 1 prime number in this range. Primes: 2 Test 4 > run Enter minimum and maximum: 4 -1 Error. Minimum and maximum must be at least 2. Enter minimum and maximum: 2 2 There is 1 prime number in this range. Primes: 2 Test 5 > run Enter minimum and maximum: 10 2 Error. Minimum must be less than maximum. Enter minimum and maximum: 2 2 There is 1 prime number in this range. Primes: 2 Test 6 > run Enter minimum and maximum: 2 3 There are 2 prime numbers in this range. Primes: 2, 3 Test 7 > run Enter minimum and maximum: 2 5 There are 3 prime numbers in this range. Primes: 2, 3, 5 Test 8 > run Enter minimum and maximum: 5 5 There is 1 prime number in this range. Primes: 5 Test 9 > run Enter minimum and maximum: 8 8 There are 0 prime numbers in this range. No primes to display. Test 10 > run Enter minimum and maximum: 3 10 There are 3 prime numbers in this range. Primes: 3, 5, 7 Test 11 > run Enter minimum and maximum: 101 149 There are 10 prime numbers in this range. Primes: 101, 103, 107, 109, 113, 127, 131, 137, 139, 149 Test 12 > run Enter minimum and maximum: 10 30 There are 6 prime numbers in this range. Primes: 11, 13, 17, 19, 23, 29 Test 13 > run Enter minimum and maximum: 2 50 There are 15 prime numbers in this range. Primes: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47 Test 14 > run Enter minimum and maximum: 14 16 There are 0 prime numbers in this range. No primes to display. Test 15 > run Enter minimum and maximum: 31398 31468 There are 0 prime numbers in this range. No primes to display.

Tasks:

TASK 3: Write a function read_range() which reads in the minimum and maximum values from the user. Define the function to have two input parameters (the minimum value of the range and the maximum value of the range) using pass-by-reference and not return a value.

The function repeatedly prompts for the minimum and maximum values until it gets two values greater than or equal to 2 and the minimum is less than or equal to the maximum. If the minimum or maximum values are less than 2, then the function prints an error message and prompts again for the two values. If the minimum value is greater than the maximum value, then the function prints an error message and prompts again for the two values.

TASK 4: Write a function is_prime() which determines if a number is prime. Define the function to have one input parameter using the const specifier. The function returns a boolean (true if the input parameter is prime and false otherwise). You will call this function in the function display_primes(). See the lecture notes on how to determine whether or not a number is a prime.

A number a is prime if and only if (a mod b 0) for b = 2, 3, , (a 1).

TASK 5: Write a function display_primes() which displays the number of prime numbers in a given range and a list of all of these prime numbers. DO NOT USE ARRAYS NOR VECTORS!. Define the function to have three input parameters using the const specifier for all parameters. Use pass-by-reference for the first parameter. Call the function is_prime() from this function. Use the following algorithm to implement the function:

a. Count the number of prime numbers in the range and determine the highest prime number in the range (if there is one). DO NOT USE ARRAYS NOR VECTORS!.

b. Display the number of prime numbers in the range. Write a different message if there is only one prime number.

c. Display the prime numbers in the range from lowest to highest separated by commas. HINT: Use the highest prime number in the range (part a) to avoid displaying a comma after the last value.

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

Step: 3

blur-text-image

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

Beginning Databases With PostgreSQL From Novice To Professional

Authors: Richard Stones, Neil Matthew

2nd Edition

1590594789, 978-1590594780

Students also viewed these Databases questions

Question

What is the difference between Needs and GAP Analyses?

Answered: 1 week ago

Question

What are ERP suites? Are HCMSs part of ERPs?

Answered: 1 week ago