1
Introduction to Programming
with Python
Sudharshan Welihinda(weli@iit.ac.lk)
Senior Lecturer, Department of Computing
Informatics Institute of Technology
Lecture 04
Exercise
2
Problem 1 : Write an algorithm to read two numbers and find their sum.
Inputs : First number num1, Second number num2.
Process : Sum = num 1 + num2
Output: Sum (sum of the two numbers.)
Algorithm:
Step1: Start
Step2: Readinput the first num1.
Step3: Readinput the second num2.
Step4: Sum = num1+num2
Step5: Write/Output Sum
Step6: End
Python program
input(‘Enter first number’)
Num2 = int(input(‘Enter second number’))
Sum = Num1 + Num2
print(Sum)
Num1 = int( )
Exercise
3
Problem 2 : Calculate area of a rectangle.
Inputs : Width & Height
Process : Area = Width * Height
Output: Area.
Problem 3 : Find the average of three numbers input by user.
Inputs : Number1 , Number2, Number3
Process : Average = (Number1 + Number2 + Nmber3) / 3
Output: Average
Exercise
4
Problem 2.1 : Write a Python program to enter length and breadth of a rectangle and
find its perimeter.
Inputs : Length & Breadth
Process : Perimeter = (Length + Breadth) * 2
Output: Perimeter.
Problem 2.2 : Write a Python program to enter two angles of a triangle and find the
third angle.
Inputs : Angle1 & Angle2
Process : ThirdAngle = 180 – (Angle1 + Angle2)
Output: ThirdAngle
Exercise
5
Problem 4 : Write a python program to convert temperature Fahrenheit to Celsius
Inputs : Temperature in Fahrenheit
Process : C = 5/9 * (F-32)
Output: Temperature in Celsius .
Problem 5 : Write a python program to Determine the total cost of apples, given the
number of kilos of apples purchased and the cost of apples per kilo.
Inputs : Number of kilos of apples
Cost of apples per kilo
Process : Total cost = Number of kilos of apples * Cost of apples per kilo
Output: Total cost of apples
Exercise
6
Problem 6 :
Write a python program to calculate Body Mass Index(BMI). Body mass index calculator
is based on the standard BMI measurement that takes your weight and height and
basically tells you if you have an unhealthy body weight or not.
The official formula, which is used in this BMI calculator, is
BMI = (weight (Kg) / (height (m) x height (m))
Inputs : Weight, Height
Process : BMI = Weight / (Height x Height)
Output: BMI (Body Mass Index)
Exercise
7
Problem 6.1 :
Write a python program to enter marks of three subjects and calculate total, average and
percentage.
Answer
8
Problem 6 – Answer(without formatting):
Weight = 0.0
Height = 0.0
BMI = 0.0
Weight = float(input('Enter your body weight... '))
Height = float(input('Enter your Height... '))
BMI = (Weight/(Height * Height))
print('Your BMI index is = ‘, BMI)
Problem 6 – Answer(with print formatting):
Weight = 0.0
Height = 0.0
BMI = 0.0
Weight = float(input('Enter your body weight... '))
Height = float(input('Enter your Height... '))
BMI = (Weight/(Height * Height))
print('Your BMI index is = ',format(BMI,'.2f'))
9
Problem 7 :
Write a Python program to read sum of money, Increase it by 8% and print the original
and the increased sum.
Exercise
Problem 8 :
Write a Python program which accepts the radius of a circle from the user and
compute the area.
Assume π = 3.14
Problem 9 :
Write a Python program which accepts the user's first and last name and print them in
reverse order with a space between them
10
Problem 7 :
OriginalSum = 0.0
IncreasedSum = 0.0
Incremental = 0.0
OriginalSum = float(input('Enter original sum... '))
Incremental = (OriginalSum* 0.08)
IncreasedSum = OriginalSum + Incremental
print('Original Sum = ',format(OriginalSum,'.2f'))
print('Increased Sum = ',format(IncreasedSum,'.2f'))
Answer
11
Answer
Problem 8 :
Radius = 0.0
Area = 0.0
Radius = float(input('Enter radius of a circle... '))
Area = Radius * Radius * 3.14
print('Are is = ',format(Area,'.2f'))
12
Answer
Problem 9 :
FirstName = 0.0
LastName = 0.0
FirstName = input('Enter first name : ')
LastName = input('Enter last name : ')
print('Full Name is - ', FirstName, ‘ ', LastName)
# print('Full Name is - ', FirstName, end=‘’)
# print(' ' , LastName)
13
Problem 10 :
Programme is to be written to input the basic salary of an employee and to calculate
and display the net salary. Net salary is to be calculated by deducting 8.5% of EPF
contribution & 1% of society contribution from the basic salary.
a) Define the memory variables needed to hold the data items in this program.
b) Write the statement that will assign the input basic salary to respective memory
variable.
c) Write the statement that will calculate the net salary.
d) Write the statement that will display the net salary.
Exercise
14
Problem 11 :
Write a Python Program to convert days into years, weeks & days.
Exercise

DOC333-2023-Sep-pythonprogramming-Lec04.pdf

  • 1.
    1 Introduction to Programming withPython Sudharshan Welihinda(weli@iit.ac.lk) Senior Lecturer, Department of Computing Informatics Institute of Technology Lecture 04
  • 2.
    Exercise 2 Problem 1 :Write an algorithm to read two numbers and find their sum. Inputs : First number num1, Second number num2. Process : Sum = num 1 + num2 Output: Sum (sum of the two numbers.) Algorithm: Step1: Start Step2: Readinput the first num1. Step3: Readinput the second num2. Step4: Sum = num1+num2 Step5: Write/Output Sum Step6: End Python program input(‘Enter first number’) Num2 = int(input(‘Enter second number’)) Sum = Num1 + Num2 print(Sum) Num1 = int( )
  • 3.
    Exercise 3 Problem 2 :Calculate area of a rectangle. Inputs : Width & Height Process : Area = Width * Height Output: Area. Problem 3 : Find the average of three numbers input by user. Inputs : Number1 , Number2, Number3 Process : Average = (Number1 + Number2 + Nmber3) / 3 Output: Average
  • 4.
    Exercise 4 Problem 2.1 :Write a Python program to enter length and breadth of a rectangle and find its perimeter. Inputs : Length & Breadth Process : Perimeter = (Length + Breadth) * 2 Output: Perimeter. Problem 2.2 : Write a Python program to enter two angles of a triangle and find the third angle. Inputs : Angle1 & Angle2 Process : ThirdAngle = 180 – (Angle1 + Angle2) Output: ThirdAngle
  • 5.
    Exercise 5 Problem 4 :Write a python program to convert temperature Fahrenheit to Celsius Inputs : Temperature in Fahrenheit Process : C = 5/9 * (F-32) Output: Temperature in Celsius . Problem 5 : Write a python program to Determine the total cost of apples, given the number of kilos of apples purchased and the cost of apples per kilo. Inputs : Number of kilos of apples Cost of apples per kilo Process : Total cost = Number of kilos of apples * Cost of apples per kilo Output: Total cost of apples
  • 6.
    Exercise 6 Problem 6 : Writea python program to calculate Body Mass Index(BMI). Body mass index calculator is based on the standard BMI measurement that takes your weight and height and basically tells you if you have an unhealthy body weight or not. The official formula, which is used in this BMI calculator, is BMI = (weight (Kg) / (height (m) x height (m)) Inputs : Weight, Height Process : BMI = Weight / (Height x Height) Output: BMI (Body Mass Index)
  • 7.
    Exercise 7 Problem 6.1 : Writea python program to enter marks of three subjects and calculate total, average and percentage.
  • 8.
    Answer 8 Problem 6 –Answer(without formatting): Weight = 0.0 Height = 0.0 BMI = 0.0 Weight = float(input('Enter your body weight... ')) Height = float(input('Enter your Height... ')) BMI = (Weight/(Height * Height)) print('Your BMI index is = ‘, BMI) Problem 6 – Answer(with print formatting): Weight = 0.0 Height = 0.0 BMI = 0.0 Weight = float(input('Enter your body weight... ')) Height = float(input('Enter your Height... ')) BMI = (Weight/(Height * Height)) print('Your BMI index is = ',format(BMI,'.2f'))
  • 9.
    9 Problem 7 : Writea Python program to read sum of money, Increase it by 8% and print the original and the increased sum. Exercise Problem 8 : Write a Python program which accepts the radius of a circle from the user and compute the area. Assume π = 3.14 Problem 9 : Write a Python program which accepts the user's first and last name and print them in reverse order with a space between them
  • 10.
    10 Problem 7 : OriginalSum= 0.0 IncreasedSum = 0.0 Incremental = 0.0 OriginalSum = float(input('Enter original sum... ')) Incremental = (OriginalSum* 0.08) IncreasedSum = OriginalSum + Incremental print('Original Sum = ',format(OriginalSum,'.2f')) print('Increased Sum = ',format(IncreasedSum,'.2f')) Answer
  • 11.
    11 Answer Problem 8 : Radius= 0.0 Area = 0.0 Radius = float(input('Enter radius of a circle... ')) Area = Radius * Radius * 3.14 print('Are is = ',format(Area,'.2f'))
  • 12.
    12 Answer Problem 9 : FirstName= 0.0 LastName = 0.0 FirstName = input('Enter first name : ') LastName = input('Enter last name : ') print('Full Name is - ', FirstName, ‘ ', LastName) # print('Full Name is - ', FirstName, end=‘’) # print(' ' , LastName)
  • 13.
    13 Problem 10 : Programmeis to be written to input the basic salary of an employee and to calculate and display the net salary. Net salary is to be calculated by deducting 8.5% of EPF contribution & 1% of society contribution from the basic salary. a) Define the memory variables needed to hold the data items in this program. b) Write the statement that will assign the input basic salary to respective memory variable. c) Write the statement that will calculate the net salary. d) Write the statement that will display the net salary. Exercise
  • 14.
    14 Problem 11 : Writea Python Program to convert days into years, weeks & days. Exercise