0% found this document useful (0 votes)
151 views8 pages

Week 2 Python Programming Exercises

The document contains 10 programs written in Python with algorithms and code snippets. Each program is summarized as follows: 1) A program that takes a name and age as input and calculates the year the person will turn 100 years old. 2) A program that takes the number of students, apples, and calculates how many apples each student gets and how many are left in the basket. 3) A program that calculates the distance between two points given their x and y coordinates. 4) A program that takes two time stamps and calculates the time difference in seconds between them. 5) A program that takes a 4-digit number as input, displays the individual digits, and calculates their sum.

Uploaded by

Manish M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
151 views8 pages

Week 2 Python Programming Exercises

The document contains 10 programs written in Python with algorithms and code snippets. Each program is summarized as follows: 1) A program that takes a name and age as input and calculates the year the person will turn 100 years old. 2) A program that takes the number of students, apples, and calculates how many apples each student gets and how many are left in the basket. 3) A program that calculates the distance between two points given their x and y coordinates. 4) A program that takes two time stamps and calculates the time difference in seconds between them. 5) A program that takes a 4-digit number as input, displays the individual digits, and calculates their sum.

Uploaded by

Manish M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

2021

Week 2: Python programs

Student Name: Manish M


SRN : PES1UG20ME172

Progra Create a program that has two parameters namely name and their age. Print out a
m1
message addressed to them that tells them the year that they will turn 100 years old.

Algorithm:
#input:name and age
#claculation:100+2021-age
#output:the year in which the age is going to be 100
Program with appropriate Comments:
name=input("enter ur name")
x=int(input("enter ur age"))
year=100+2021-x
print("the year when ",name,"is going to be 100 years old is",year)
Out Put Screen shot:

Progra N students take K apples and distribute them among each other evenly. The remaining
m2
(the undivisible) part remains in the basket. How many apples will each single student
get? How many apples will remain in the basket?The program reads the
numbers N and K. It should print the two answers for the questions above.

Algorithm:
#input: no. of students and no. of apples
#calculation: n//k, n%k
#output: no. of apples each student gets, no. of apples remaining in the basket

Program with appropriate Comments:


n=int(input("enter the no. of students"))
k=int(input("no. of apples in the basket"))

1 Department of Computer Science & Engg, PESU


2021
Week 2: Python programs

x=n//k
y=n%k
print("the no. of apples each student gets is",x)
print("the no. of apples left in the basket is",y)

Out Put Screen shot:

Progra Write a program to calculate the distance between two points.


m3

Algorithm:
#input:coordinates of the two points
#calculation:((a-b)^2+(c-d)^2)^0.5
#output:distance b/w two points

Program with appropriate Comments:

import math
a=int(input("enter the x coordinate of the first point"))
c=int(input("enter the y coordinate of the first point"))
b=int(input("enter the x coordinate of the second point"))
d=int(input("enter the y coordinate of the second point"))
x=((a-b)**2+(c-d)**2)
y=math.pow(x,0.5)
print("distance b/w the two points is",y)

Out Put Screen shot:

2 Department of Computer Science & Engg, PESU


2021
Week 2: Python programs

Progra Given two timestamps of the same day: a number of hours, minutes and seconds for
m4
both of the timestamps. The moment of the first timestamp happened before the
moment of the second one. Calculate how many seconds passed between them.

Algorithm:
#input=initial time and final time
#calculation=conversion of hours and minutes to second and difference of it.
#output=seconds b\w the time

Program with appropriate Comments:


a=int(input("enter the initial hour"))
b=int(input("enter the initial minutes"))
c=int(input("enter the initial seconds"))
d=int(input("enter the final hour"))
e=int(input("enter the final minutes"))
f=int(input("enter the final seconds"))

x=((d-a)*3600)+((e-b)*60)+(f-c)

print("the seconds b\w the given time is",x)

Out Put Screen shot:

Progra Given a 4-digit integer number, display the individual digits & also compute the sum of
m5
digits.

Algorithm:
#input=for digit no.
#calculation=addition of each digit
#output=sum of each digit

3 Department of Computer Science & Engg, PESU


2021
Week 2: Python programs

Program with appropriate Comments:


a=int(input("enter a 4 digit no."))
b=a%10
c=(a//10)%10
d=(a//100)%10
e=(a//1000)%10
x=b+c+d+e
print("the given digits are",e,d,c,b)
print("the sum of given digits are",x)
Out Put Screen shot:

Progra Swap the contents of two memory locations


m6
a) using temporary variable.
b) without using temporary variable.

Algorithm:
#input=two digits
#output=swaped value for the given variables

Program with appropriate Comments:

a=int(input("enter the value of a"))


b=int(input("enter the value of b"))
a,b=b,a
print("the value of a=",a,"the value of b=",b)

#with temproary variable


x=a
a=b
b=x
print("the value of a=",a,"the value of b=",b)
Out Put Screen shot:

4 Department of Computer Science & Engg, PESU


2021
Week 2: Python programs

Progra Program to
m7
a) Convert temperature in celsius to fahrenheit
b) Convert temperature in fahrenheit to celsius

Algorithm:
#input=temperature in celcius
#calculation=(celcius*(9/5))+32
#output=temperature in farenheit

#input=temperature in farenheit
#calculation=(farenheit-32)*(5/9)
#output=temperature in celcius

Program with appropriate Comments:


x=int(input("enter the temperature in celcius"))
y=(x*1.8)+32
print("the temperature in farenheit",y)

a=int(input("enter the temperature in farenheit"))


b=(a-32)*(5/9)
print("the temperature in celsius",b)
Out Put Screen shot:

Progra Given the distance between 2 cities in kilometers. Write a Python program convert it
m8
into meters, centimeters, feet and inches and display the result.

Algorithm:

5 Department of Computer Science & Engg, PESU


2021
Week 2: Python programs

#input=distance b\w 2 cities


#calculation=conversion from kms to m,cm,feet,inches
#output=distance in m,cm,feet,inches

Program with appropriate Comments:


x=int(input("enter the distance b\w two cities"))
m=x*1000
cm=m*100
feet=cm*30.84
inch=cm*2.54
print("distance b\w two cities in metre=",m,",cm=",cm,",feet=",feet,",inches=",inch)
Out Put Screen shot:

Progra A school decided to replace the desks in three classrooms. Each desk sits two students.
m9
Given the number of students in each class, print the smallest possible number of
desks that can be purchased.The program should read three integers: the number of
students in each of the three classes, a, b and c respectively.In the first test there are
three groups. The first group has 20 students and thus needs 10 desks. The second
group has 21 students, so they can get by with no fewer than 11 desks. 11 desks is also
enough for the third group of 22 students. So we need 32 desks in total.

Algorithm:
#input=no. of students
#calculation=dividing the benches b\w students
#output=no. of benches needed
Program with appropriate Comments:
a=int(input("no. of students in class 1="))
b=int(input("no. of students in class 2="))
c=int(input("no. of students in class 3="))
q=(a//2)+(a%2)
w=(b//2)+(b%2)
e=(c//2)+(c%2)
x=q+w+e
print("the total no. of benches needed is ",x)
print("no. of benches requiered in class 1=",q,"class 2=",w,"class 3=",e)

6 Department of Computer Science & Engg, PESU


2021
Week 2: Python programs

Out Put Screen shot:

Progra Given the integer N - the number of seconds that is passed since midnight - how many
m 10
full hours and full minutes are passed since midnight? The program should print two
numbers: the number of hours (between 0 and 23) and the number of minutes
(between 0 and 1339). For example, if N = 3900, then 3900 seconds have passed since
midnight. Therefore, the time now is 1:05am. So the program should print 1 65 - 1 full
hour is passed since midnight, 65 full minutes passed since midnight.

Algorithm:
#input=number of seconds passed since midnight
#calculation=conversion of second to minutes and hours
#output=time in hours and minitues
Program with appropriate Comments:
n=int(input("enter the second passed since midnight"))
x=n//3600
y=(n%3600)//60
z=(n%3600)%60
print("the current time is",x,":",y,":",z)
print("hours passed is",x,",minutes passed",y,",seconds passed",z)
Out Put Screen shot:

7 Department of Computer Science & Engg, PESU


2021
Week 2: Python programs

8 Department of Computer Science & Engg, PESU

You might also like