You are on page 1of 10

Computer Organization

Lab #4
Dr Sajid Gul Khawaja
LE Kashaf Raheem
Algorithms and Repetition
Aim
Exploring python syntax and problem-
solving w.r.t repetition structures

2
Example:
› Write a Python program to display all
integers within the range 100-200 whose
sum of digits is an even number.
Example:

QUESTION SOLUTION
› Write a program that reads 10 count = 0
positive numbers from the
keyboard and determines and sum = 0
displays the sum and average of while (count < 10):
the numbers number = input("Input a new
number: ");
sum = sum + int(number)
count = count + 1
print ("Sum of 10 numbers is: ",
sum)

4
Looping “Forever”

PROBLEM SOLUTION
› Use a value that will always be while True:
true to loop until a “break”.
string = input()
if string==“exit”:
break
print (string)
print ("Done”)

5
Tasks
Make sure to comment your code.
Use appropriate variable name
Know the location where file is being saved
Create different files for different tasks

6
Task 1:
› Write a program that keeps getting and adding
the number (unless it is multiple of 5) until
negative number is entered. Display the final
sum as output.

Hint: Make use of nested if/else within while

7
Task 2:
› Write a Python program that accepts an integer (n)
and computes the value of 𝒏 + (𝒏 + 𝒏) + (𝒏 + 𝒏 +
𝒏) + … (𝒏 + 𝒏 + 𝒏 … + 𝒏)

8
Note: The characters are arranged
in descending order
Task 3:
› You are required to create a python program which can
convert numbers entered in ROMAN Numerals into decimal
numbers. E.g., user entered MLXVI number becomes 1066
(1000+50+10+5+1)

ROMAN Numeral Decimal Value


M 1000
D 500
C 100
L 50
X 10
V 5
I 1
9
Next week: Python Syntax
and Problem Solving!
Advice:
Explore Leetcode for problem solving!

27

You might also like