Part 1: Writing Exercise:

The following are the review questions from chapter 4 in your book.

Page 184: Do review exercises: R4.2 (a, b, c, d) & R.4.4 (a, b, c, f)

The answers to the questions from page 184 in your book should be typed in the block of comments in the java file such as;

/*
R4.2 a


R4.4 f
*/

R4.2 Write a loop that computes

  • The sum of all even numbers between 2 and 100 (inclusive)
  • The sum of all squares between 1 and 100 (inclusive)
  • The sum of all odd numbers between a and b (inclusive).
  • The sum of all odd digits of n. (For example, if n is 32677, the sum would be 3 + 7 + 7 = 17)

R4.4 What do these loops print?

  • for (int i = 1; i < 10; i++) { System.out.print(i + " "); }
  • for(int i = 1; i < 10; i += 2) { System.out.print(i + " "); }
  • for(int i = 10; i > 1; i--) { System.out.print(i + " "); }
  • for(int i = 0; i < 10; i++) { System.out.print(i + " "); }
  • for(int i = 0; i < 10; i = i * 2) { System.out.print(i + " "); }
  • for(int i = 1; i < 10; i++) { if(i % 2 == 0) { System.out.print(i + " "); }}

Part 2: Programming

Write a program called Assignment4 (saved in a file Assignment4.java) that reads in a sequence of positive integers until a user enters 0 to quit (we don't know how many numbers the user may enter). The loop continues to process input values until the user enters zero: Here is sample of the program execution in Bold letters.

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 , minimum among the integers, the sum, the count, and the average of these integers which were input by the user. DO NOT include 0 in the computation which is the last integer read by the program.

(Note that max, min, sum, and count will be computed through iterations of a loop, see chapter 4 for example the Writing a Loop on page 169)

Your average should be formatted to exactly 2 digits (to the right of the decimal point).

Hint: There are two parts to this program. The first part reads a sequence of positive numbers and keeps track of the maximum, minimum, sum, count, and average.

The second part is, for each positive integer, 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.

Hint: Write a separate program to find if a number is prime or not, and then include it to the first part mentioned above.

Example Execution:

The following is an example input and output. The input is shown in red.

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.50
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.