8️⃣

8.8 Practice

Chapter Table of Contents

Ch. 8 Loops

Section Table of Contents

Practice

Factorial - For Loop

Create a class called FactorialFor which takes an integer >= 0 and prints the factorial of that integer. (The factorial of 4 is 4 * 3 * 2 * 1 = 24. Factorial is denoted with an exclamation point, like so: 4! = 24.) You should use a for loop to calculate the factorial.

Sample output:

Entner an integer: 5
5! = 120

Factorial - While Loop

Practice:

Create a class called FactorialWhile which takes an integer >= 0 and prints the factorial of that integer. (The factorial of 4 is 4 * 3 * 2 * 1 = 24. Factorial is denoted with an exclamation point, like so: 4! = 24.) You should use a while loop to calculate the factorial.

Sample output:

Entner an integer: 5
5! = 120

CountAverage

Write a program called CountAverage that reads an unspecified number of integers, determines how many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a decimal. You can assume that the user will enter at least 1 non-zero number.

Sample output:

Enter integers separated by spaces. Input ends if it is 0: 1 2 -1 3 0
The number of positives is 3
The number of negatives is 1
The total is 5
The average is 1.25

Adapted from Exercise 5.1 from Introduction to Java Programming (Comprehensive), 10th ed. by Y. Daniel Liang

Prime

Create a program called Prime which prompts the user to enter the number of prime numbers they wish to have displayed (let this be numberOfPrimes). Then display the first numberOfPrimes prime numbers such that there are 10 primes displayed per line.

Sample output:

Enter the number of primes you want to print: 15
The first 15 prime numbers are

2 3 5 7 11 13 17 19 23 29
31 37 41 43 47

💡

Note: An integer is prime if it is greater than 1 and its only positive divisor is 1 or itself. For example, 2, 3, 5, and 7 are prime, but 4, 6, 8, and 9 are not.

Hint: This problem is difficult and requires you to solve multiple problems. The key to this is to break it down into the following tasks:

  • Determine whether a given number is prime
  • For number = 2, 3, 4, 5, 6 ... , etc., test whether it is prime
  • Count the prime numbers (so that you can print 10 on each line)
  • Display each prime number, and display 10 numbers per line

We highly recommend that for this problem, you plan out your program rather than writing it out by scratch right out of the gate. Programmers often do this with a technique called pseudocode, where you write your program out with comments in regular English about what you plan to do.

Adapted from Listing 5.15 PrimeNumber.java from Introduction to Java Programming (Comprehensive), 10th ed. by Y. Daniel Liang

Previous Section

7️⃣
8.7 Infinite Loop

Next Section

🕓
Chapter 8 Quiz

⚖️

Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.