Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Enter a positive integer. Enter 0 to quit. For each positive integer, check if it is a prime number or not. If it is, print

Enter a positive integer. Enter 0 to quit.

For each positive integer, check if it is a prime number or not. If it is, print out:

The number X is a prime number.

If it is not, print out:

The number X is not a prime number.

Note that X is a positive number read from user input.

Once user enters 0, then stop asking for more positive integers, and compute the maximum and minimum among the read integers, and the sum, the count, and the average of these integers. These computations DO NOT include 0 -- the last read number.
(note that max, min, sum, and count will be computed through iterations of a loop)
Your average should be formatted so that exactly 3 digits appears to the right of the decimal point.

Hint:
There are two parts to this program. The one reads a sequence of positive numbers and finds the maximum, minimum, sum, count, and average. This can be written by imitating the example in Listing 4.6 (chapter 4).

The other part is, for each positive integer, we need to determine if it is a prime number or not. A prime number (integer) is an integer that can be divided only by itself and 1. For example, 7 is a prime number and 6 is not (6 can be divided by 2 and 3). We can check if an integer can be divided by another integer using the remainder operator (%). If the remainder is 0, it can be divided. And we need to check this for all integers between 2 and n-1 for a positive integer n. You can write a program to compute this for one integer. Once you finish it, you can put it together with the first part mentioned above.



Sample Output: (the integers entered by a user are shown in bold)



Enter a positive integer. Enter 0 to quit.
43
The number 43 is a prime number.
Enter a positive integer. Enter 0 to quit.
6
The number 6 is not a prime number.
Enter a positive integer. Enter 0 to quit.
7
The number 7 is a prime number.
Enter a positive integer. Enter 0 to quit.
9
The number 9 is not a prime number.
Enter a positive integer. Enter 0 to quit.
12
The number 12 is not a prime number.
Enter a positive integer. Enter 0 to quit.
4
The number 4 is not a prime number.
Enter a positive integer. Enter 0 to quit.
0

The maximum positive number is: 43
The minimum positive number is: 4
The sum is: 81
The count of number(s) is: 6
The average is: 13.500

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 Programming questions