Sequential Problems

Reading Time: 6 minutes


Printing Information

Write a program in Python that takes input from user his/her details and then prints it back on screen.

Enter your name:   Abhimanyu
Enter your gender: M
Enter your age:    16
Enter your height: 213.36

Your name is:   Abhimanyu
Your gender is: M
Your age is:    16
Your height is: 213.36

Welcome Human!
name = input('Enter your name:   ')
gender = input('Enter your gender: ')
age = int(input('Enter your age:    '))
height = float(input('Enter your height: '))

print('Your name is:   ', name)
print('Your gender is: ', gender)
print('Your age is:    ', age)
print('Your height is: ', height)

print('Welcome Human!')

Geometrical Problems

Review programs in Python to solve simple geometrical problems.

Finding Perimeter and Area

Write a program in Python that takes length L and width W of a rectangle from user and calculates the perimeter and area of the rectangle.

\[ \begin{aligned} \text{Perimeter of a rectangle} &= 2 \times (\text{length} + \text{width}) \\ \text{Area of a rectangle} &= \text{length} \times \text{width} \\ \end{aligned} \]

Enter length of rectangle: 5
Enter width of rectangle:  7

Perimeter of rectangle is: 24
Area of rectangle is:      35
l = int(input('Enter length of rectangle: '))
w = int(input('Enter width of rectangle:  '))

p = 2 * (l + w)
a = l * w

print('Perimeter of rectangle is: ', p)
print('Area of rectangle is:      ', a)

Finding Circumference and Area

Write a program in Python that takes radius R of a circle from user and calculates the circumference and area of the circle.

\[ \begin{aligned} \text{Circumference of a circle} &= 2 \times \pi \times \text{radius} \\ \text{Area of a circle} &= \pi \times \text{radius}^2 \\ \pi &= 3.14 \end{aligned} \]

Enter radius of circle: 5

Circumference of circle is: 31.42
Area of circle is:          78.54
from math import pi

r = int(input('Enter radius of circle: '))

c = 2 * pi * r
a = pi * r * r

print('Circumference of circle is: ', c)
print('Area of circle is:          ', a)

Finding Surface Area and Volume

Write a program in Python that takes one side S of a cube from user and calculates the volume and surface area of the cube.

\[ \begin{aligned} \text{Volume of a cube} &= \text{side} \times \text{side} \times \text{side} \\ \text{Surface area of a cube} &= 6 \times \text{side} \times \text{side} \\ \end{aligned} \]

Enter side of cube: 4

Surface area of cube is: 96
Volume of cube is:       64
s = int(input('Enter side of cube: '))

a = 6 * (s ** 2)
v = s ** 3

print('Surface area of cube is: ', a)
print('Volume of cube is:       ', v)

Percentage Problems

Review programs in Python to solve simple real-life percentage related problems.

Finding Percentage of Marks

Class teachers have a tiring task of calculating the percentage of marks obtained in science subjects for each students. To help them out, your class decides to ease their task.

Write a program in Python that takes marks obtained by a student in Biology B, Physics P, and Chemistry C, along with the total marks T of all the three subject and calculate the percentage of marks obtained in science subjects by a student.

\[ \text{Marks obtained} = \frac{\text{Marks obtained in (Biology + Physics + Chemistry)}}{\text{Total marks of science subjects}} \times 100 \]

Enter marks obtained in Biology:   80
Enter marks obtained in Chemistry: 78
Enter marks obtained in Physics:   85
Enter total marks of all subjects: 300

Marks obtained in Science subjects: 81
b = int(input('Enter marks obtained in Biology:   '))
c = int(input('Enter marks obtained in Chemistry: '))
p = int(input('Enter marks obtained in Physics:   '))
t = int(input('Enter total marks of all subjects: '))

percent = ((b + c + p) / t) * 100

print('Marks obtained in Science subjects: ', percent)

Finding Simple Interest

One of your rich relative is in a business of giving out loans. Being in a loan business, he often has to calculate simple interest. So, you decided to ease his task of finding interest payable by clients.

Write a program in Python that takes from user a principal amount P, annual rate of interest R, and time of loan T and calculates the simple interest and total amount payable at the end of loan period.

\[ \begin{aligned} \text{Interest} &= \text{Principal} \times \text{Time} \times \frac{Rate}{100} \\ \text{Amount} &= \text{Interest} + \text{Principal} \\ \end{aligned} \]

Enter principal amount: 25000
Enter interest rate:    2.5
Enter time elapsed:     3

Interest payable:     1875
Total amount payable: 26875
p = float(input('Enter principal amount: '))
r = float(input('Enter interest rate:    '))
t = float(input('Enter time elapsed:     '))

i = (p * r * t) / 100
a = p + i

print('Interest payable:     ', i)
print('Total amount payable: ', a)

Finding Gain Percentage

Sneha owns a stationery shops and wants to make a certain gain on each item she sells. Help her out by writing a program for her.

Write a program in Python for her that takes the cost price CP of an item and the gain percentage GP she wants and then calculate the selling price of the item.

\[ \text{Selling price} = \text{Cost price} \times \left( 1 + \frac{\text{Gain \%}}{100} \right) \]

Enter cost price of item: 50
Enter gain percentage:    30

Selling price will be: 65
cp = float(print('Enter cost price of item: '))
gp = float(print('Enter gain percentage:    '))

sp = cp * (1 + (gp / 100))

print('Selling price will be: ', sp)

SI Unit Problems

SI units is the future. Yet, people still tend to use the old-school British sytem of units instead of the new standards of units. To help people out and make remarkable change on the society, you decided to write a bunch of programs to convert older units to newer SI units.

Converting Gram to Kilogram

Write a program in Python that takes a weight G in grams from user and prints the corresponding weight in Kilogram.

Enter weight (in G): 2500

Weight (in KG) is: 2.5
g = float(input('Enter weight (in G): '))

kg = g / 1000

print('Weight (in KG) is: ', kg)

Converting Fahreinheit to Celsius

Write a program in Python that takes a temperature F in Fahreinheit from user and prints the corresponding temperature in Celsius.

Enter temperature (in F): 98.2

Temperature (in C) is: 36.8
f = float(input('Enter temperature (in F): '))

c = 5 * (f - 32) / 9

print('Temperature (in C) is: ', c)

Converting H:M:S to Seconds

Write a program in Python that takes hours H, minutes M, and seconds S from user and prints the corresponding time in seconds.

Enter hour:    42
Enter minutes: 14
Enter seconds: 5

Total seconds: 152045
h = int(input('Enter hour:    '))
m = int(input('Enter minutes: '))
s = float(input('Enter seconds: '))

s = s + (m * 60) + (h * 60 * 60)

print('Total seconds: ', s)