Tutorial 3 - Iteration Stuctures
Tutorial 3 - Iteration Stuctures
Tutorial n° 3
Iteration structures
Exercise 1:
Let x be a real and n an integer, given by the user. Write an algorithm that calculates x raised
to the power of n.
Exercise 2:
Write an algorithm to calculate the sum of one element out of three in the sequence of integers,
starting at the value 2 (2+5+8+11+…) and stopping at 100.
Give two distinct solutions using For and While loops.
Exercise 3:
Write an algorithm to display one element out of three in the sequence of integers, starting at
the value 2 and stopping at 100. The algorithm will also calculate the sum of the elements of
this sequence which are divisible by 5.
Exercise 4:
Calculate and display the first n terms of the Fibonacci sequence, n given by the user.
A Fibonacci sequence is a sequence in which each element is the sum of the two elements that
precede it: Fi=Fi-1+Fi-2, with Fi the ith term of the sequence. The first two terms are by
definition equal to 1:
F0=1
F1=1
Exercise 5 :
Using the For and While loops, write an algorithm that reads a positive integer n from the
keyboard, calculates and displays the sum:
S = 1 + 1/2 + 1/4 + … + 1/2n
Exercise 6:
1) Read a positive integer in base 10, convert it to its equivalent in binary base then display
the result.
2) Generalize the algorithm to convert a positive integer in base 10, given by the user, into a
base b (2 <= b < 10) also given by the user.
Exercise 7:
Write an algorithm that determines whether an integer N is perfect or not. An integer is
perfect if it is equal to the sum of its divisors except itself.
Example: 6=3+2+1.
Exercise 8:
1) Write an algorithm that checks and displays if a number nb given by the user is prime or
not.
2) Write an algorithm that finds and displays the first n prime numbers, knowing that a prime
number is a positive integer divisible only by 1 and by itself.
Exercise 9 :
Write an algorithm that takes an integer and determines how many times it is divisible by 2.
Example:
11 is divisible 0 times by 2
4 is divisible 2 times by 2
8 is divisible 3 times by 2
Exercise 10:
Write an algorithm that allows to:
i) Read an integer N (with 2 <= N <= 20)
ii) Read N pairs of strictly positive real numbers X and Y and determine the largest ratio
X/Y.
iii) Display the largest ratio and the corresponding X and Y values.
Exercise 11:
Consider the following expression:
S = 1+2(1+2) + 3(1+2+3) + … + n(1+2+…+n)
Write a complete algorithm that allows to:
i) Read an integer n with n ≥ 2.
ii) Calculate and display the value of S.
Exercise 12:
Write an algorithm that reads reals representing the general averages of the N students in a
class, knowing that each student has only one general average. The number of students is
entered from the keyboard (5<=N<=20). Write an algorithm to perform the following
processing:
1) Enter the different averages of the N students (an average must be between 0 and 20).
3) Determine the number of students who have a qualification: a student is qualified if his
less than 6.
6) Calculate the average of the general averages that are greater than or equal to 10.
Exercise 13:
Write an algorithm that allows to enter strictly positive integers in ascending order, i.e. a
value is only accepted if it is strictly greater than the previous value already accepted. The
entry stops when the user enters the value 0 and the algorithm will display the number of
accepted values.