Professional Documents
Culture Documents
Practical3 Term2 Friday
Practical3 Term2 Friday
COS101 2023
Term 2 – Practical 3 Friday
This practical focuses on writing Python programs that use lists, Boolean expressions, logical and
numerical operators. Furthermore, the practical aims to give you more practice on functions,
loops, and conditional statements.
Output:
Welcome to our Simple Calculator Program
Enter A for Addition
Enter S for Subtraction
Enter M for Multiplication
Enter Q to Quit. A
Part 1
Geometric Progression (GP) Program: Write a python program that uses a function to output a
GP given the initial term (never zero), common ratio and the number of terms in the sequence.
A GP is a sequence of elements in which the next number/term in the sequence is obtained by
multiplying the previous number/term by the common ratio. For example:
• 1,2,4,8,16: The initial term is 1, the common ratio is 2 and there are 5 terms in the
sequence.
• 2,6,18,54: The initial term is 2, the common ratio is 3 and there are 4 terms in the sequence.
Generally, the next number in the sequence is obtained by using this formula:
a_n = a_1 * r**(n – 1)
where a_n = next number in the sequence, a_1 = first number in the sequence, r= common ratio,
n = number of terms
The program should be able to produce output similar to the example below.
Output:
Please enter the initial value a: 2
Please enter the common ratio r: 4
Part 2
Modify the program above to also output the sum of the GP. The sum of the numbers in a GP is
obtained using any of these formulae:
If r = 1, sum = a * n
If r != 1 and r >1, sum = a((rn - 1)/ (r – 1))
If r != 1 and r <1, sum = a((1 – rn )/ (1 - r))
The program should be able to produce output similar to the example below.
Output:
Please enter the initial value a: 1
Write a Python program that outputs the pyramid of numbers using nested loops.
The program should use a function that can output any pyramid when given any integer and have
an output similar to the one below.
Output:
Enter the number of rows 8
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7 7
8 8 8 8 8 8 8 8