




































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
A comprehensive introduction to repetition and loop statements in programming. It explores the concept of the greatest common divisor (gcd) of two numbers and presents a step-by-step algorithm for calculating it using repeated division. The document also includes a practical example of a weather report program that demonstrates how to use loops to process data and count occurrences. Well-structured and includes clear explanations, code examples, and visual representations, making it an excellent resource for learning about repetition and loop statements.
Typology: Cheat Sheet
1 / 44
This page cannot be seen from the preview
Don't miss anything!
The greatest common divisor (GCD) of two
integers is the product of the integers’
common factors. Write a program that inputs
two numbers and find their GCD by repeated
division. For example, consider the numbers
252 and 735. find the remainder of one
divided by the other.
Input Output Logic Involved
Two numbers GCD of the
numbers
Euclidean algorithm,
binary GCD algorithm,
repeated division
method
Step 1: Read the numbers from the user
Step 2: Let dividend = number1 and divisor =
number
Step 3: Repeat step 4 to step 6 while remainder not
equal to zero
Step 4: remainder = number1 modulus number
Step 5: dividend = divisor
Step 6: divisor = remainder
Step 7: GCD = divisor
Step 8: print GCD
Input Output Logic Involved
Rainfall in cm
recorded for ten
days, ten
numbers
Number of
days low,
medium
and high
rainfall
Count the number of
days with rainfall as
low, medium and high
Step 1: Let counter =0, num_Of_Low = 0, num_Of_Med =
0, num_Of_High = 0
Step 2: If counter == 10 then goto Step 9
Step 3: Read rainfall recorded in cm
Step 4: increment counter
Step 5: if rainfall < 12 then increment num_Of_Low
Step 6: if rainfall is between 12 and 20 then increment
num_Of_Med
Step 7:Step 5: if rainfall > 20 then increment num_Of_High
Step 8: Goto Step 2
Step 9: Print num_Of_Low, num_Of_Med, num_Of_High
Used when we can determine prior to loop
execution exactly how many loop repetitions will be
needed to solve the problem
A counter-controlled loop follows this general
format:
Set loop control variable to an initial value of 0
while (loop control variable < final value)...
Increase loop control variable by 1
GCD Program Using While
Weather Program Using While
Weather Program Using For