The For Loop
The For Loop
Michelle M. Khalifé
Assignment 3 – for & while
Ref.:
https://press.rebus.community/programmingfundamentals/chapter/for-loop/
https://www.zenflowchart.com/blog/for-loop-flowchart
Ref.:
https://press.rebus.community/programmingfundamentals/chapter/while-loop/
https://www.quora.com/Whats-the-origin-of-while-loops (fun/historical/educational read)
For each exercise, please do:
#1 Write a program that prints all the numbers between 1 and 10 – as well as their sum.
#2 Write a program to print all the characters whose decimal values range between 33 and 126. (ASCII table)
#3 Write a program that prints all the numbers from 0 to 6 except 3 and 6.
#4 Write a program to print the numbers between 1500 and 2700, such that they are divisible by 5 and 7.
#5 Write a program that prompts the user to input an integer. Validate the input, then print this number’s
multiplication table.
E.g.:
1xm=…
2xm=…
3xm=…
4xm=…
.
.
.
10xm = …
#6 Write a program that prompts the user for a positive integer. Validate the input, then let the user know whether
this number is prime or not. A prime number is only divisible by 1 and itself.
#7 Write a program that prompts the user for two positive integers. Validate the input, then find the value of one
number raised to the power of another.
E.g.:
2, 3 = 2 x 2 x 2 = 8
4, 2 = 4 x 4 = 16
#8 Write a program that prompts the user for a positive integer and finds the factorial value of a positive integer
entered through the keyboard. The factorial of a number n is calculated by multiplying the number by all the
numbers that come before it – down to 1.
n! = n x (n-1)!
E.g.:
7! = 7 x 6 x 5 x 4 x 3 x 2 x 1
5! = 5 x 4 x 3 x 2 x 1
3! = 3 x 2 x 1
…
#9 Write a program to guess a number between 1 and 9. If the user guesses wrong, then the prompt appears again
until the guess is correct. On a successful guess, the user gets a “Well guessed!” message, and the program will exit.
Modify the program to cap the number of guesses.
#10 Write a program that reads a set of integers, and then prints the following: the number of even integers, the
number of odd integers, the sum of the even integers, and the sum of the odd integers.