You are on page 1of 1

ASSIGNMENT 2

ALGORITHM

Exercise 1

Greatest common divisor (GCD) of two integers n1 and n2, which are not all zero, is the largest positive
integer that divides each of the integers. To find the GCD, smaller integer is subtracted from the larger
integer, and the result is assigned to the variable holding larger integer. This process is continued
until n1 and n2 are equal. For example, n1=18, n2=81

1. n2 > n1

n2= n2 - n1 = 81 – 18 = 63

2. n2 > n1

n2= n2 - n1 = 63 -18=45

3. n2 > n1

n2 = n2 - n1 = 45-18 =27

4. n2 > n1

n2 = n2 - n1 = 27 - 18 = 9

5. n1 > n2

n1 = n 1- n2 = 18-9 = 9

6. n1 = n2, stop

GCD of 18 and 81 is 9

Build an algorithm to compute greatest common divisor for 2. Express the algorithm with flowchart and
pseudo code

Exercise 2

Build an algorithm to enter a list of integers. Find and print the largest value in the list. Print the indexes
of the largest elements. Indexes start with 1. Express the algorithm with flowchart and pseudo code.

For example:

Input: Number of element in the list: 8

The list: 1, 4, 8, 2,8,3, -1,8

Expected result:

Largest value: 8

Indexes of largest elements: 3, 5, 8

You might also like