PRACTICE
MORE LOOP EXERCISES
Exercises
- A factorial is the product of all of the integers between 1 and a given natural number. e.g: 5! = 5 * 4 * 3 * 2 * 1 = 120.
Write a program that calculates the factorial of a given number input by the user.
- Write a program that prints the numbers from 1 to 100. But for multiples of three, print "Fizz" instead of the number, and for the multiples of five, print "Buzz". For numbers which are multiples of both three and five, print "FizzBuzz".
- The Fibonnaci sequence is a sequence of numbers where each number is the sum of the two previous numbers
Fn = Fn-1 + Fn-2, and F1 = F2 = 1.
e.g.: 1, 1, 2, 3, 5, 8, 13 …
Write a program that asks the user for a positive integer N, and prints out the first N Fibonacci numbers to generate. The program should reject all invalid inputs from the user (i.e. N must be a natural number).
- Write a program that generates a random number, and ask the user to guess repeatedly until they guess it right. Tell the user "too high" or "too low", as appropriate, after each guess. Keep track of how many guesses the user needs to find the random number, and print out their results at the end.
- Write a program that prompts the user for natural numbers until the sentinel value “stop” is input. After “stop” is input, print out the second-highest number that the user inputted. You may assume that the user will always input 3 or more valid numbers before “stop”.