# While loop
###### tags: `C++ 基礎練習`
## 1. Factorial Calculator
Write a program that prompts the user to enter a number, n. The program should then use a while loop to calculate the factorial of n (i.e. n! = n * (n-1) * (n-2) * ... * 1). The program should display the factorial to the user.
Input: 5
Output: 120
## 2. Positive Number Average
Write a program that prompts the user to enter a series of numbers, terminated by a negative number. The program should then use a while loop to calculate the average of the positive numbers entered by the user. The program should display the average to the user.
Input: 5 10 -3
Output: 7.5
## 3. Guessing Game
Write a program that generates a random number between 1 and 100. The program should then prompt the user to guess the number, and use a while loop to give the user hints (i.e. "too high" or "too low") until the user guesses the correct number. The program should display the number of guesses it took the user to guess the number.
```
Guess a number between 1 and 100: 50
Too high!
Guess a number between 1 and 100: 25
Too low!
Guess a number between 1 and 100: 37
Too high!
Guess a number between 1 and 100: 31
Too low!
Guess a number between 1 and 100: 34
Congratulations! You guessed the number in 5 tries.
```
## 4. Sum of Digits
Write a program that prompts the user to enter a number, n. The program should then use a while loop to calculate the sum of the digits of n (i.e. if n = 123, the sum would be 1 + 2 + 3 = 6). The program should display the sum to the user.
Input: 1 2 3
Output: 6
## 5. Maximum Value
Write a program that prompts the user to enter a series of integers, terminated by a 0. The program should then use a while loop to find the maximum value in the series. The program should display the maximum value to the user.
Input: 2 5 1 10 0
Output: 10