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

💻 Template code

✅ Solution code

Factorial - While Loop

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

💻Template code

✅ Solution code

CountAverage

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

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.