0% found this document useful (0 votes)
15 views

PythonPrograms 1

This document contains 22 examples of Python code to solve various problems involving mathematical calculations. The examples cover topics like calculating sums, products, averages, areas, volumes, temperatures, heights and more. Complete Python code is provided for each example problem.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

PythonPrograms 1

This document contains 22 examples of Python code to solve various problems involving mathematical calculations. The examples cover topics like calculating sums, products, averages, areas, volumes, temperatures, heights and more. Complete Python code is provided for each example problem.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Python Programming Class : 9

Output: Programs:

Ex. 1: Write Python code to find the Sum of two numbers

# sum of two numbers


a = int(input("Enter First Number : "))
b = int(input("Enter Second Number :"))
c=a+b
print("Sum of two numbers :", c)

Output: Ex. 2: Write Python code to find the product of three numbers

# product of three numbers


a = int(input("Enter First Number :"))
b = int(input("Enter Second Number :"))
c = int(input("Enter Third Number :"))
d=a*b*c
print("The product of three numbers :", d)

Ex. 3: Write Python code to accept three subject marks from the user and find
calculate Total and Average.
Output:
# Calculate Total and Average
mark1 = int(input("Enter Subject mark 1 :"))
mark2 = int(input("Enter Subject mark 2 :"))
mark3 = int(input("Enter Subject mark 3 :"))
total = mark1 + mark2 + mark3
average = total / 3
print("Total Marks :", total)
print("Average Marks :",average)
Output: Ex. 4: Write Python code to calculate Simple Interest.

# calculate simple interest


p = float(input("Enter Principle :"))
n = float(input("Enter Number of Years :"))
r = float(input("Enter Rate of interest :"))
si = (p * n * r) / 100
print("Simple Interest :", si)

Output: Ex. 5: Write Python code to find the Perimeter of a Rectangle.

# perimeter of a rectangle
l = float(input("Enter Length of a rectangle :"))
b = float(input("Enter Breadth of a rectangle :"))
p = 2 * (l + b)
print("Perimeter of a rectangle :", p)

Output: Ex. 6: Write Python code to find the Area of a Circle.

# area of a circle
r = float(input("Enter radius of a circle :"))
a = 3.14 * r ** 2
print("Area of a circle :", a)

Output: Ex. 7: Write Python code to find the Area of Triangle.

# area of a triangle
b = float(input("Enter base of a triangle :"))
h = float(input("Enter height of a triangle :"))
a = 0.5 * b * h
print("Area of a triangle :", a)
Output: Ex. 8: Write Python code to find the Area of a Rectangle.

# area of a rectangle
l = float(input("Enter length of a rectangle :"))
b = float(input("Enter breadth of a rectangle :"))
a=l*b
print("Area of a rectangle:", a)

Output: Ex. 9: Write Python code to find the Circumference of a Circle.

# circumference of circle
r = float(input("Enter Radius of a circle :"))
c = 2 * 3.14 * r
print("Circumference of a circle :", c)

Output:
Ex. 10: Write Python code to find the Perimeter of a Square.

# perimeter of a square
s = float(input("Enter side of a square :"))
p=4*s
print("Perimeter of a square :", p)

Output:
Ex. 11: Write Python code to find the Volume of a Sphere.

# volume of a sphere
r = float(input("Enter radius of a sphere :"))
v = (4 / 3) * 3.14 * r * r * r
print("volume of a sphere :", v)
Output:
Ex. 12: Write Python code to find the Volume of the Cylinder.

# volume of the cylinder


r = float(input("Enter radius of a cylinder :"))
h = float(input("Enter height of a cylinder :"))
v = 3.14 * r * r * h
print("Volume of a cylinder :", v)
Ex. 13: Write a program in Python to input two numbers in two variables, swap
the values of the two variables, and then display the swapped values.
Output:
a = eval(input("Enter the first number ="))
b = eval(input("Enter the second number ="))
print ("The initial values are: a = ", a, " b = ",b)
t=a
a=b
b=t
print ("After swapping, the values are: a = " ,a," b = ", b)
Ex. 14: Write a program in Python to accept a number as input from the user and
display the square and cube of the number.
Output:
# Square and Cube of a number
a = eval(input("Enter a number : "))
square = a ** 2
cube = a** 3
print ("Square of a number = ", square)
print ("Cube of a number = ",cube)
Ex. 15: Write a program in Python to input a temperate value in Celsius and
display the same in Fahrenheit.
Output :
# Calculate temperature in Fahrenheit from Celsius
C = eval(input("Enter the temperature in Celsius :"))
F = C * 9/5 + 32
print ("Temperature in Fahrenheit = ", F
Ex. 16: Write a program in Python to enter your height in centimetres, convert it
in feet and inches, and display it.
Output:
# To input your height in centimetres and
# display in feet and inches.
height = int(input("Enter your height in centimetres : "))
height_inch = height /2.54
feet = height_inch // 12
inch = height_inch % 12
print ("Your height is", feet, "feet | ", inch, "inches")
Ex. 17: Write a Python program to calculate the EMI (equated monthly
instalment) according to the formula given below.
Output:
EMI = ((p*r/12) (1+r/12)**n) / ((1+r/12)**n – 1)
# To input principal amount, rate of interest, total number
# of months, calculate and display EMI
p = eval(input("Enter Principal amount:"))
r = eval(input("Enter Interest rate:"))
n = eval(input("Enter Total number of months:"))
EMI = (p*r/12) * ((1+r/12)**n) / ((1+r/12) **n-1)
print ("EMI =", EMI)

Output: Ex. 18: Write a program in Python to enter the restaurant bill amount and display
the amount to be paid after deducting 10% discount on the bill and adding
taxes, CGST and SGST at 2.25% each.

# To input bill amount and display the amount to be paid


# after discount and GST
bill_amt = eval(input("Enter Bill amount:"))
dis = 0.10 * bill_amt
dis_bill_amt = bill_amt - dis
cgst = 0.025 * dis_bill_amt
sgst = cgst
amt_paid = dis_bill_amt + cgst + sgst
print ("Bill Amount ≈", bill_amt)
print ("Discount (10%) =", dis)
print("Bill amount after discount =", dis_bill_amt)
print ("CGST (2.5%) =", cgst)
print ("SGST (2.5%) =", sgst)
print ("Amount to be paid =", amt_paid)
Ex. 19: Write a program in Python to input three numbers and calculate the
following expression:
Output:

# To input three numbers and calculate the expression


a = eval(input("Enter first number:"))
b = eval(input("Enter second number:"))
c = eval(input("Enter third number:"))
X = (a**3 + b**3 + c**3) ** (1/2)
print ("X = ", X)

Ex. 20: The computer science teacher has asked the students to write a Python
program to input the length of the three sides of a triangle and display its
area using the formula given below. (Here, a, b and care the lengths of the
three sides of a triangle; S is the semi-perimeter and A is the area):

Output:

# Three sides of the triangle - a, b and c


import math
a = float(input("Enter side a:"))
b = float(input("Enter side b:"))
c = float(input("Enter side c:"))
# calculate the semi-perimeter
S = (a+b+c)/2
# calculate the area
Area = math.sqrt(S*(S-a)*(S-b)*(S-c))
print ("Value of Semi-Perimeter= ",S)
print ("Area of triangle= ",Area)
Ex. 21: The mathematics teacher has given the problem to students to calculate
the total cost of fencing along a field of length X metres and breadth Y
Output: metres. The cost of one metre of fencing is RS.1000. Take the value of X
and Y from the user.

# Cost of Fencing
X = int(input("Enter field of Length - X:"))
Y = int(input("Enter field of Breadth - Y:"))
# Cost of one meter = Rs.1000
Z = (X*1000) + (Y*1000)
print ("Cost of fencing = ",Z)

Output: Ex. 22: The AXZ Company has given an increase of 8.5% in salary to its employees.
Write a python program to input the salary and display the new salary.

OldSalaryPerMonth = int(input("Enter your old salary per month:"))


hike = float(input("Enter your hike percentage:"))
presentSalaryPerMonth = OldSalaryPerMonth + (OldSalaryPerMonth * hike / 100)
print("After hike your present salary per month is: ", presentSalaryPerMonth)

You might also like