0% found this document useful (0 votes)
33 views

The For Loop

The document provides instructions for an assignment on for and while loops in programming. It explains that for loops are used when repeating an action a specific number of times over a range of numbers, while while loops are used for repeating an action an unknown number of times or modifying a control variable. The assignment includes 10 exercises to practice using for and while loops to solve different programming problems.

Uploaded by

Lê Thư
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

The For Loop

The document provides instructions for an assignment on for and while loops in programming. It explains that for loops are used when repeating an action a specific number of times over a range of numbers, while while loops are used for repeating an action an unknown number of times or modifying a control variable. The assignment includes 10 exercises to practice using for and while loops to solve different programming problems.

Uploaded by

Lê Thư
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

La Salle College

Algorithms & Programming (420AP1AS)


_______________________________________________________________________________________________

Michelle M. Khalifé
Assignment 3 – for & while

the for loop


Use it when …
you need to repeat an action n number of times, where n is either specified or deduced
you need to iterate over a specific range of numbers

Ref.:
https://press.rebus.community/programmingfundamentals/chapter/for-loop/
https://www.zenflowchart.com/blog/for-loop-flowchart

the while loop


Use it when …
you need to repeat an action an unknown number of times
you need to modify the control variable through a non-standard increment
you need to read a file (more on this later … )

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:

o Determine the best-candidate: for loop or while loop


o Use the appropriate loop to solve the question
o Use the other loop to solve the question

#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.

You might also like