100 Python Functions Programs COllection 2083
4.2
User-defined Functions
Program 1: Write a
Python program to input two numbers and calculate their sum using a
user-defined function.
def add(a, b):
return a + b
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
s = add(a, b)
print(f"Sum of {a} and {b} is {s}")
Program 2: Write a
Python program to input two numbers and calculate their difference using a
user-defined function.
def subtract(a, b):
return a - b
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
d = subtract(a, b)
print(f"Difference of {a} and {b} is {d}")
Program 3: Write a
Python program to input two numbers and calculate their product using a
user-defined function.
def multiply(a, b):
return a * b
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
p = multiply(a, b)
print(f"Product of {a} and {b} is {p}")
Program 4: Write a Python program to
input two numbers and calculate their quotient using a user-defined function.
def divide(a, b):
return a / b
a = float(input("Enter first
number: "))
b = float(input("Enter second
number: "))
if b != 0:
q = divide(a, b)
print(f"Quotient of {a} and {b} is {q}")
else:
print("Division by zero is not allowed.")
Program 5: Write a
Python program to input two numbers and calculate the average using a
user-defined function.
def average(a, b):
return (a +
b) / 2
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
avg = average(a, b)
print(f"Average of {a} and {b} is {avg}")
Program 6: Write a
Python program to input a number and calculate its square using a user-defined
function.
def square(n):
return n * n
n = int(input("Enter a number: "))
s = square(n)
print(f"Square of {n} is {s}")
Program 7: Write a
Python program to input a number and calculate its cube using a user-defined
function.
def cube(n):
return n * n
* n
n = int(input("Enter a number: "))
c = cube(n)
print(f"Cube of {n} is {c}")
Program 8: Write a
Python program to input a number and calculate its square root using a
user-defined function.
def square_root(n):
return n **
0.5
n = float(input("Enter a number: "))
if n >= 0:
sr =
square_root(n)
print(f"Square root of {n} is {sr}")
else:
print("Square root of a negative number is not real.")
Program 9: Write a
Python program to input a number and calculate its cube root using a
user-defined function.
def cube_root(n):
return n **
(1/3)
n = float(input("Enter a number: "))
cr = cube_root(n)
print(f"Cube root of {n} is {cr}")
Program 10: Write a
Python program to input two numbers and calculate their sum, difference,
product, quotient, and average using a single user-defined function.
def calculate(a, b):
s = a + b
d = a - b
p = a * b
avg = (a +
b) / 2
if b != 0:
q = a /
b
else:
q =
"Not possible"
return s, d,
p, q, avg
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
s, d, p, q, avg = calculate(a, b)
print(f"Sum of {a} and {b} is {s}")
print(f"Difference of {a} and {b} is {d}")
print(f"Product of {a} and {b} is {p}")
print(f"Quotient of {a} and {b} is {q}")
print(f"Average of {a} and {b} is {avg}")
Program 11: Write a
Python program to input a number and calculate its square, cube, square root
and cube root using a single user-defined function.
def calculate(n):
s = n ** 2
c = n ** 3
sr = n **
0.5
cr = n **
(1/3)
return s, c,
sr, cr
n = float(input("Enter a number: "))
s, c, sr, cr = calculate(n)
print(f"Square of {n} is {s}")
print(f"Cube of {n} is {c}")
print(f"Square root of {n} is {sr}")
print(f"Cube root of {n} is {cr}")
Program 12: Write a Python program to
convert temperature from Celsius to
Fahrenheit using a user-defined function. Formula: F = (C * 9/5) + 32
def convert(c):
return (c *
9/5) + 32
c = float(input("Enter temperature in Celsius:
"))
f = convert(c)
print(f"{c}°C is equal to {f}°F")
Program 13: Write a Python program
to convert temperature from Fahrenheit to Celsius using a user-defined
function. Formula: C = (F - 32) * 5/9
def convert(f):
return (f -
32) * 5/9
f = float(input("Enter temperature in Fahrenheit:
"))
c = convert(f)
print(f"{f}°F is equal to {c}°C")
Program 14: Write a Python program to convert distance from
kilometres to metres using a user-defined function.
Formula: 1 km = 1000 m
def convert(km):
return km *
1000
km = float(input("Enter distance in kilometres:
"))
m = convert(km)
print(f"{km} kilometres is equal to {m}
metres")
Program 15: Write a Python program to convert distance from metres to
kilometres using a user-defined function.
Formula: 1 km = 1000 m
def convert(m):
return m /
1000
m = float(input("Enter distance in metres:
"))
km = convert(m)
print(f"{m} metres is equal to {km}
kilometres")
Program 16: Write a
Python program to convert length from metres to centimetres using a
user-defined function.
Formula: 1 m = 100 cm
def convert(m):
return m *
100
m = float(input("Enter length in metres: "))
cm = convert(m)
print(f"{m} metres is equal to {cm}
centimetres")
Program 17: Write a
Python program to convert length from centimetres to metres using a
user-defined function.
Formula: 1 m = 100 cm
def convert(cm):
return cm /
100
cm = float(input("Enter length in centimetres:
"))
m = convert(cm)
print(f"{cm} centimetres is equal to {m}
metres")
Program 18: Write a
Python program to convert weight from kilograms to grams using a user-defined
function.
Formula: 1 kg = 1000 g
def convert(kg):
return kg *
1000
kg = float(input("Enter weight in kilograms:
"))
g = convert(kg)
print(f"{kg} kilograms is equal to {g}
grams")
Program 19: Write a
Python program to convert weight from grams to kilograms using a user-defined
function.
Formula: 1 kg = 1000 g
def convert(g):
return g /
1000
g = float(input("Enter weight in grams: "))
kg = convert(g)
print(f"{g} grams is equal to {kg}
kilograms")
Program 20: Write a Python program to
convert time from hours to minutes using a user-defined function.
Formula: 1 hour = 60 minutes
def convert(h):
return h *
60
h = float(input("Enter time in hours: "))
m = convert(h)
print(f"{h} hours is equal to {m} minutes")
Program 21: Write a Python program to
convert time from seconds to minutes using a user-defined function.
Formula: 1 minute = 60 seconds
def convert(s):
return s /
60
s = float(input("Enter time in seconds: "))
m = convert(s)
print(f"{s} seconds is equal to {m}
minutes")
Program 22: Write a Python program to
convert time from days to hours using a user-defined function.
Formula: 1 day = 24 hours
def convert(d):
return d *
24
d = float(input("Enter number of days: "))
h = convert(d)
print(f"{d} days is equal to {h} hours")
Program 23: Write a Python program to
convert length from inches to centimetres using a user-defined function.
Formula: 1 inch = 2.54 cm
def convert(i):
return i *
2.54
i = float(input("Enter length in inches: "))
cm = convert(i)
print(f"{i} inches is equal to {cm}
centimetres")
Program 24: Write a Python program to
convert the given number of days into years, months and remaining days using a
user-defined function.
Formula: 1 year = 365 days
1 month = 30 days
def convert(d):
y = d // 365
r = d % 365
m = r // 30
days = r %
30
return y, m,
days
d = int(input("Enter number of days: "))
y, m, days = convert(d)
print(f"{d} days = {y} years, {m} months and
{days} days")
Program 25: Write a Python program to
convert time in seconds into hours, minutes and remaining seconds using a
user-defined function.
Formula: 1 hour = 3600 seconds
1 minute = 60 seconds
def convert(s):
h = s //
3600
r = s % 3600
m = r // 60
sec = r % 60
return h, m,
sec
s = int(input("Enter time in seconds: "))
h, m, sec = convert(s)
print(f"{s} seconds = {h} hours, {m} minutes and
{sec} seconds")
Program 26: Write a Python program to calculate
simple interest using a user-defined function.
Formula: SI = (P * T * R) / 100
def simple_interest(p, t, r):
return (p *
t * r) / 100
p = float(input("Enter principal: "))
t = float(input("Enter time in years: "))
r = float(input("Enter rate of interest: "))
si = simple_interest(p, t, r)
print(f"Simple Interest for principal {p}, time
{t} years and rate {r}% is {si}")
Program 27: Write a
Python program to calculate the total amount after adding simple interest to
the principal using a user-defined function.
Formula: Amount =
Principal + Simple Interest
def amount(p, si):
return p +
si
p = float(input("Enter principal: "))
si = float(input("Enter simple interest: "))
print("Total Amount =", amount(p, si))
Program 28: Write a
Python program to calculate the total amount using principal, time and rate of
interest with a user-defined function.
Formula: Amount = P +
(P * T * R) / 100
def calculate_amount(p, t, r):
si = (p * t
* r) / 100
return p +
si
p = float(input("Enter principal: "))
t = float(input("Enter time in years: "))
r = float(input("Enter rate of interest: "))
print("Total Amount =", calculate_amount(p,
t, r))
Program 29: Write a
Python program to calculate discount amount using a user-defined function.
Formula: Discount =
(Price * Discount Rate) / 100
def discount(price, rate):
return
(price * rate) / 100
price = float(input("Enter price: "))
rate = float(input("Enter discount rate: "))
print("Discount Amount =", discount(price,
rate))
Program 30: Write a
Python program to calculate selling price after discount using a user-defined
function.
Formula: Selling Price
= Price - Discount
def selling_price(price, rate):
d = (price *
rate) / 100
return price
- d
price = float(input("Enter price: "))
rate = float(input("Enter discount rate: "))
print("Selling Price =",
selling_price(price, rate))
Program 31: Write a
Python program to calculate profit using a user-defined function.
Formula: Profit =
Selling Price - Cost Price
def profit(sp, cp):
return sp -
cp
cp = float(input("Enter cost price: "))
sp = float(input("Enter selling price: "))
print("Profit =", profit(sp, cp))
Program 32: Write a
Python program to calculate loss using a user-defined function.
Formula: Loss = Cost
Price - Selling Price
def loss(cp, sp):
return cp -
sp
cp = float(input("Enter cost price: "))
sp = float(input("Enter selling price: "))
print("Loss =", loss(cp, sp))
Program 33: Write a
Python program to calculate profit percentage using a user-defined function.
Formula: Profit
Percentage = (Profit / Cost Price) * 100
def profit_percentage(cp, sp):
return ((sp
- cp) / cp) * 100
cp = float(input("Enter cost price: "))
sp = float(input("Enter selling price: "))
print("Profit Percentage =",
profit_percentage(cp, sp))
Program 34: Write a
Python program to calculate loss percentage using a user-defined function.
Formula: Loss
Percentage = (Loss / Cost Price) * 100
def loss_percentage(cp, sp):
return ((cp
- sp) / cp) * 100
cp = float(input("Enter cost price: "))
sp = float(input("Enter selling price: "))
print("Loss Percentage =",
loss_percentage(cp, sp))
Program 35: Write a
Python program to calculate the area of a rectangle using a user-defined
function.
Formula: Area = Length
* Breadth
def area(l, b):
return l * b
l = float(input("Enter length: "))
b = float(input("Enter breadth: "))
print("Area of rectangle =", area(l, b))
Program 36: Write a
Python program to calculate the perimeter of a rectangle using a user-defined
function.
Formula: Perimeter = 2
* (Length + Breadth)
def perimeter(l, b):
return 2 *
(l + b)
l = float(input("Enter length: "))
b = float(input("Enter breadth: "))
print("Perimeter of rectangle =",
perimeter(l, b))
Program 37: Write a
Python program to calculate the area of a square using a user-defined function.
Formula: Area = Side *
Side
def area(a):
return a * a
a = float(input("Enter side: "))
print("Area of square =", area(a))
Program 38: Write a
Python program to calculate the perimeter of a square using a user-defined
function.
Formula: Perimeter = 4
* Side
def perimeter(a):
return 4 * a
a = float(input("Enter side: "))
print("Perimeter of square =", perimeter(a))
Program 39: Write a
Python program to calculate the area of a circle using a user-defined function.
Formula: Area = 3.14 *
Radius * Radius
def area(r):
return 3.14
* r * r
r = float(input("Enter radius: "))
print("Area of circle =", area(r))
Program 40: Write a
Python program to calculate the circumference of a circle using a user-defined
function.
Formula: Circumference
= 2 * 3.14 * Radius
def circumference(r):
return 2 *
3.14 * r
r = float(input("Enter radius: "))
print("Circumference of circle =",
circumference(r))
Program 41: Write a
Python program to calculate the area of a triangle using a user-defined
function.
Formula: Area = (Base *
Height) / 2
def area(b, h):
return (b *
h) / 2
b = float(input("Enter base: "))
h = float(input("Enter height: "))
print("Area of triangle =", area(b, h))
Program 42: Write a
Python program to calculate the perimeter of a triangle using a user-defined
function.
Formula: Perimeter = a
+ b + c
def perimeter(a, b, c):
return a + b
+ c
a = float(input("Enter first side: "))
b = float(input("Enter second side: "))
c = float(input("Enter third side: "))
print("Perimeter of triangle =",
perimeter(a, b, c))
Program 43: Write a
Python program to calculate the area of a triangle using Heron's formula with a
user-defined function.
Formula: s = (a + b + c) / 2
Area = √(s * (s - a) * (s - b) * (s -
c))
def area(a, b, c):
s = (a + b +
c) / 2
return (s *
(s - a) * (s - b) * (s - c)) ** 0.5
a = float(input("Enter first side: "))
b = float(input("Enter second side: "))
c = float(input("Enter third side: "))
print("Area of triangle =", area(a, b, c))
Program 44: Write a
Python program to calculate the area of four walls of a room using a
user-defined function.
Formula: Area of Four
Walls = 2 * (Length + Breadth) * Height
def area(l, b, h):
return 2 *
(l + b) * h
l = float(input("Enter length: "))
b = float(input("Enter breadth: "))
h = float(input("Enter height: "))
print("Area of four walls =", area(l, b, h))
Program 45: Write a
Python program to calculate the volume of a cylinder using a user-defined
function.
Formula: V = 3.14 * r *
r * h
def volume(r, h):
return 3.14
* r * r * h
r = float(input("Enter radius: "))
h = float(input("Enter height: "))
print("Volume of cylinder =", volume(r, h))
Program 46: Write a
Python program to calculate the curved surface area of a cylinder using a
user-defined function.
Formula: CSA = 2 * 3.14
* r * h
def csa(r, h):
return 2 *
3.14 * r * h
r = float(input("Enter radius: "))
h = float(input("Enter height: "))
print("Curved Surface Area of cylinder =",
csa(r, h))
Program 47: Write a Python program to
calculate the total surface area of a cylinder using a user-defined function.
Formula: TSA = 2 * 3.14 * r * (r + h)
def tsa(r, h):
return 2 *
3.14 * r * (r + h)
r = float(input("Enter radius: "))
h = float(input("Enter height: "))
print("Total Surface Area of cylinder =",
tsa(r, h))
Program 48: Write a Python program to
calculate the volume of a sphere using a user-defined function.
Formula: V = (4/3) * 3.14 * r * r * r
def volume(r):
return (4/3)
* 3.14 * r ** 3
r = float(input("Enter radius: "))
print("Volume of sphere =", volume(r))
Program 49: Write a Python program to
calculate the surface area of a sphere using a user-defined function.
Formula: SA = 4 * 3.14 * r * r
def surface_area(r):
return 4 *
3.14 * r * r
r = float(input("Enter radius: "))
print("Surface Area of sphere =",
surface_area(r))
Program 50: Write a Python program to
calculate the volume of a hemisphere using a user-defined function.
Formula: V = (2/3) * 3.14 * r * r * r
def volume(r):
return (2/3)
* 3.14 * r ** 3
r = float(input("Enter radius: "))
print("Volume of hemisphere =", volume(r))
Program 51: Write a Python program to
calculate the curved surface area of a hemisphere using a user-defined
function.
Formula: CSA = 2 * 3.14 * r * r
def csa(r):
return 2 *
3.14 * r * r
r = float(input("Enter radius: "))
print("Curved Surface Area of hemisphere =",
csa(r))
Program 52: Write a
Python program to calculate the total surface area of a hemisphere using a
user-defined function.
Formula: TSA = 3 * 3.14
* r * r
def tsa(r):
return 3 *
3.14 * r * r
r = float(input("Enter radius: "))
print("Total Surface Area of hemisphere =",
tsa(r))
Program 53: Write a Python program to
calculate the volume of a cuboid using a user-defined function.
Formula: V = l * b * h
def volume(l, b, h):
return l * b
* h
l = float(input("Enter length: "))
b = float(input("Enter breadth: "))
h = float(input("Enter height: "))
print("Volume of cuboid =", volume(l, b, h))
Program 54: Write a Python program to
calculate the total surface area of a cuboid using a user-defined function.
Formula: TSA = 2 * (l*b + b*h + h*l)
def tsa(l, b, h):
return 2 *
(l*b + b*h + h*l)
l = float(input("Enter length: "))
b = float(input("Enter breadth: "))
h = float(input("Enter height: "))
print("Total Surface Area of cuboid =",
tsa(l, b, h))
Program 55: Write a Python program to
calculate the volume of a cube using a user-defined function.
Formula: V = a * a * a
def volume(a):
return a **
3
a = float(input("Enter side: "))
print("Volume of cube =", volume(a))
Program 56: Write a Python program to
calculate the total surface area of a cube using a user-defined function.
Formula: TSA = 6 * a * a
def tsa(a):
return 6 * a
* a
a = float(input("Enter side: "))
print("Total Surface Area of cube =",
tsa(a))
Program 57: Write a Python program to
calculate the volume, curved surface area and total surface area of a cylinder
using a single user-defined function.
Formula:
Volume = 3.14 * r * r * h, CSA = 2 *
3.14 * r * h, TSA = 2 * 3.14 * r * (r + h)
def calculate(r, h):
v = 3.14 * r
* r * h
csa = 2 *
3.14 * r * h
tsa = 2 *
3.14 * r * (r + h)
return v,
csa, tsa
r = float(input("Enter radius: "))
h = float(input("Enter height: "))
v, csa, tsa = calculate(r, h)
print("Volume of cylinder =", v)
print("Curved Surface Area of cylinder =",
csa)
print("Total Surface Area of cylinder =",
tsa)
Program 58: Write a Python program to
calculate the volume, curved surface area and total surface area of a
hemisphere using a single user-defined function.
Formula:
Volume = (2/3) * 3.14 * r * r * r
CSA = 2 * 3.14 * r * r
TSA = 3 * 3.14 * r * r
def calculate(r):
v = (2/3) *
3.14 * r ** 3
csa = 2 *
3.14 * r * r
tsa = 3 *
3.14 * r * r
return v,
csa, tsa
r = float(input("Enter radius: "))
v, csa, tsa = calculate(r)
print("Volume of hemisphere =", v)
print("Curved Surface Area of hemisphere =",
csa)
print("Total Surface Area of hemisphere =",
tsa)
Program 59: Write a Python program to
calculate the volume, curved surface area and total surface area of a cylinder
using a single user-defined function.
Formula:
Volume = 3.14 * r * r * h
CSA = 2 * 3.14 * r * h
TSA = 2 * 3.14 * r * (r + h)
def calculate(r, h):
v = 3.14 * r
* r * h
csa = 2 *
3.14 * r * h
tsa = 2 *
3.14 * r * (r + h)
return v,
csa, tsa
r = float(input("Enter radius: "))
h = float(input("Enter height: "))
v, csa, tsa = calculate(r, h)
print("Volume of cylinder =", v)
print("Curved Surface Area of cylinder =",
csa)
print("Total Surface Area of cylinder =",
tsa)
Program 60: Write a Python program to
calculate the volume, curved surface area and total surface area of a cone
using a single user-defined function.
Formula:
Volume = (1/3) * 3.14 * r * r * h
CSA = 3.14 * r * l
TSA = 3.14 * r * (r + l)
def calculate(r, h, l):
v = (1/3) *
3.14 * r * r * h
csa = 3.14 *
r * l
tsa = 3.14 *
r * (r + l)
return v,
csa, tsa
r = float(input("Enter radius: "))
h = float(input("Enter height: "))
l = float(input("Enter slant height: "))
v, csa, tsa = calculate(r, h, l)
print("Volume of cone =", v)
print("Curved Surface Area of cone =", csa)
print("Total Surface Area of cone =", tsa)
Program 61: Write a
Python program to input a number and check whether it is even or odd using a
user-defined function.
def check(n):
if n % 2 ==
0:
return
"Even"
else:
return
"Odd"
n = int(input("Enter a number: "))
print(f"{n} is an {check(n)} number.")
Program 62: Write a
Python program to check whether a number is positive, negative or zero using a
user-defined function.
def check(n):
if n > 0:
return
"Positive"
elif n <
0:
return
"Negative"
else:
return
"Zero"
n = float(input("Enter a number: "))
print(f"The number is {check(n)}.")
Program 63: Write a Python program to
check whether a person is eligible to vote using a user-defined function.
def check(age):
if age >=
18:
return
"You are eligible to vote"
else:
return
"You are not eligible to vote"
age = int(input("Enter your age: "))
print(check(age))
Program 64: Write a Python program to
find the greater number between two numbers using a user-defined function.
def greater(a, b):
if a > b:
return a
elif b >
a:
return b
else:
return
"Both numbers are equal"
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Greater number =", greater(a, b))
Program 65: Write a Python program to
find the smaller number between two numbers using a user-defined function.
def smaller(a, b):
if a < b:
return a
elif b <
a:
return b
else:
return
"Both numbers are equal"
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Smaller number =", smaller(a, b))
Program 66: Write a Python program to
find the greatest among three numbers using a user-defined function.
def greatest(a, b, c):
if a >= b
and a >= c:
return a
elif b >=
a and b >= c:
return b
else:
return c
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
print("Greatest number =", greatest(a, b,
c))
Program 67: Write a Python program to
find the middle number among three numbers using a user-defined function.
def middle(a, b, c):
if (a >=
b and a <= c) or (a <= b and a >= c):
return a
elif (b
>= a and b <= c) or (b <= a and b >= c):
return b
else:
return c
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
print("Middle number =", middle(a, b, c))
Program 68: Write a Python program to
check whether a given year is a leap year or not using a user-defined function.
def leap_year(year):
if year %
400 == 0 or (year % 4 == 0 and year % 100 != 0):
return
"Leap year"
else:
return
"Not a leap year"
year = int(input("Enter a year: "))
print(f"{year} is a {leap_year(year)}.")
Program 69: Write a Python program to
check whether a number is divisible by both 5 and 11 using a user-defined
function.
def check(n):
if n % 5 ==
0 and n % 11 == 0:
return
"Divisible by both 5 and 11"
else:
return
"Not divisible by both 5 and 11"
n = int(input("Enter a number: "))
print(f"{n} is {check(n)}.")
Program 70: Write a Python program to
check whether a student has passed or failed using a user-defined function.
def result(marks):
if marks
>= 40:
return
"Passed"
else:
return
"Failed"
marks = float(input("Enter marks: "))
print(f"Student has {result(marks)}.")
Program 71: Write a Python program to
check whether a person is eligible for a driving licence using a user-defined
function.
def check(age):
if age >=
18:
return
"Eligible for driving licence"
else:
return
"Not eligible for driving licence"
age = int(input("Enter your age: "))
print(f"You are {check(age)}.")
Program 72: Write a Python program to
calculate the grade of a student based on marks using a user-defined function.
def grade(marks):
if marks
>= 90:
return
"A+"
elif marks
>= 80:
return
"A"
elif marks
>= 70:
return
"B"
elif marks
>= 60:
return
"C"
elif marks
>= 40:
return
"D"
else:
return
"F"
marks = float(input("Enter marks: "))
print(f"Your grade is {grade(marks)}.")
Program 73: Write a Python program to
find the smallest among three numbers using a user-defined function.
def smallest(a, b, c):
if a <= b
and a <= c:
return a
elif b <=
a and b <= c:
return b
else:
return c
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
print("Smallest number =", smallest(a, b,
c))
Program 74: Write a Python program to
check whether a number is divisible by 3 or 7 using a user-defined function.
def check(n):
if n % 3 ==
0 or n % 7 == 0:
return
"Divisible by 3 or 7"
else:
return
"Not divisible by 3 or 7"
n = int(input("Enter a number: "))
print(check(n))
Program 75: Write a Python program to
check whether three given sides can form a valid triangle using a user-defined
function.
def check(a, b, c):
if a > 0
and b > 0 and c > 0 and (a + b > c) and (a + c > b) and (b + c >
a):
return
"Valid triangle"
else:
return
"Invalid triangle"
a = float(input("Enter first side: "))
b = float(input("Enter second side: "))
c = float(input("Enter third side: "))
print(check(a, b, c))
Program 76: Write a Python program to
calculate the electricity bill based on units consumed using a user-defined
function.
First 20 units: Rs. 5 per unit
Next 30 units: Rs. 7 per unit
Above 50 units: Rs. 10 per unit
def electricity_bill(units):
if units
<= 20:
return
units * 5
elif units
<= 50:
return
(20 * 5) + (units - 20) * 7
else:
return
(20 * 5) + (30 * 7) + (units - 50) * 10
units = float(input("Enter electricity units:
"))
print("Electricity bill = Rs.",
electricity_bill(units))
Program 77: Write a Python program to
calculate the discount and final amount based on purchase amount using a
user-defined function.
Rs. 5000 or above: 20% discount
Rs. 3000 or above: 10% discount
Rs. 1000 or above: 5% discount
Below Rs. 1000: No discount
def discount(amount):
if amount
>= 5000:
return
amount * 20 / 100
elif amount
>= 3000:
return
amount * 10 / 100
elif amount
>= 1000:
return
amount * 5 / 100
else:
return 0
amount = float(input("Enter purchase amount:
"))
d = discount(amount)
print("Discount amount = Rs.", d)
print("Final amount = Rs.", amount - d)
Program 78: Write a Python program to
calculate the water bill based on units consumed using a user-defined function.
First 10 units: Rs. 10 per unit
Next 10 units: Rs. 15 per unit
Above 20 units: Rs. 20 per unit
def water_bill(units):
if units
<= 10:
return
units * 10
elif units
<= 20:
return
(10 * 10) + (units - 10) * 15
else:
return
(10 * 10) + (10 * 15) + (units - 20) * 20
units = float(input("Enter water units: "))
print("Total water bill = Rs.",
water_bill(units))
Program 79: Write a Python program to
calculate the bonus of an employee based on years of service using a
user-defined function.
10 years or more: 20% bonus
5 years or more: 10% bonus
Below 5 years: 5% bonus
def bonus(salary, years):
if years
>= 10:
return
salary * 20 / 100
elif years
>= 5:
return
salary * 10 / 100
else:
return
salary * 5 / 100
salary = float(input("Enter monthly salary:
"))
years = int(input("Enter years of service:
"))
b = bonus(salary, years)
print("Bonus amount = Rs.", b)
print("Total salary = Rs.", salary + b)
Program 80: Write a Python program to
calculate taxi fare based on distance travelled using a user-defined function.
Criteria:
First 5 km: Rs. 50 per km
Next 5 km: Rs. 40 per km
Above 10 km: Rs. 30 per km
def taxi_fare(distance):
if distance
<= 5:
return
distance * 50
elif
distance <= 10:
return
(5 * 50) + (distance - 5) * 40
else:
return
(5 * 50) + (5 * 40) + (distance - 10) * 30
distance = float(input("Enter distance travelled
in km: "))
print("Total taxi fare = Rs.",
taxi_fare(distance))
Program 81: Write a Python program to
calculate the sum of digits of a number using a user-defined function.
def sum_digits(n):
s = 0
while n >
0:
r = n %
10
s = s +
r
n = n //
10
return s
n = int(input("Enter a number: "))
print("Sum of digits =", sum_digits(n))
Program 82: Write a Python program to
count the number of digits in a given number using a user-defined function.
def count_digits(n):
count = 0
while n >
0:
count =
count + 1
n = n //
10
return count
n = int(input("Enter a number: "))
print("Number of digits =", count_digits(n))
Program 83: Write a Python program to
reverse a given number using a user-defined function.
def reverse(n):
rev = 0
while n >
0:
r = n %
10
rev =
rev * 10 + r
n = n //
10
return rev
n = int(input("Enter a number: "))
print("Reverse number =", reverse(n))
Program 84: Write a Python program to
check whether a number is a palindrome or not using a user-defined function.
def palindrome(n):
temp = n
rev = 0
while n >
0:
r = n %
10
rev =
rev * 10 + r
n = n //
10
if temp ==
rev:
return
"Palindrome"
else:
return
"Not Palindrome"
n = int(input("Enter a number: "))
print(f"{n} is {palindrome(n)}.")
Program 85: Write a Python program to
calculate the product of digits of a number using a user-defined function.
def product_digits(n):
p = 1
while n >
0:
r = n %
10
p = p *
r
n = n //
10
return p
n = int(input("Enter a number: "))
print("Product of digits =",
product_digits(n))
Program 86: Write a Python program to
calculate the factorial of a number using a user-defined function.
def factorial(n):
f = 1
for i in
range(1, n + 1):
f = f *
i
return f
n = int(input("Enter a number: "))
print("Factorial =", factorial(n))
Program 87: Write a
Python program to check whether a number is an Armstrong number or not using a
user-defined function.
def armstrong(n):
temp = n
s = 0
digits =
len(str(n))
while n >
0:
r = n %
10
s = s +
r ** digits
n = n //
10
if temp ==
s:
return
"Armstrong number"
else:
return
"Not an Armstrong number"
n = int(input("Enter a number: "))
print(f"{n} is {armstrong(n)}.")
Program 88: Write a Python program to
check whether a number is a prime number or not using a user-defined function.
def prime(n):
if n <=
1:
return
"Not a prime number"
for i in
range(2, n):
if n % i
== 0:
return "Not a prime number"
return
"Prime number"
n = int(input("Enter a number: "))
print(f"{n} is a {prime(n)}.")
Program 89: Write a
Python program to display the multiplication table of a given number using a
user-defined function.
def table(n):
for i in
range(1, 11):
print(f"{n} x {i} = {n * i}")
n = int(input("Enter a number: "))
table(n)
Program 90: Write a Python program to
calculate the sum of first n natural numbers using a user-defined function.
Formula: Sum = 1 + 2 + 3 + ... + n
def total(n):
s = 0
for i in
range(1, n + 1):
s = s +
i
return s
n = int(input("Enter a number: "))
print("Sum =", total(n))
Program 91: Write a Python program to
calculate the HCF of two numbers using a user-defined function.
def hcf(a, b):
while b !=
0:
a, b =
b, a % b
return a
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("HCF =", hcf(a, b))
Program 92: Write a Python program to
calculate the LCM of two numbers using a user-defined function.
Formula: LCM = (First Number * Second Number) / HCF
def lcm(a, b):
x = a
y = b
while y !=
0:
x, y =
y, x % y
return (a *
b) // x
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("LCM =", lcm(a, b))
Program 93: Write a Python program to
display all factors of a given number using a user-defined function.
def factors(n):
for i in
range(1, n + 1):
if n % i
== 0:
print(i)
n = int(input("Enter a number: "))
print("Factors are:")
factors(n)
Program 94: Write a Python program to
display the Fibonacci series up to n terms using a user-defined function.
def fibonacci(n):
a = 0
b = 1
for i in
range(n):
print(a,
end=" ")
a, b =
b, a + b
n = int(input("Enter number of terms: "))
fibonacci(n)
Program 95: Write a Python program to
check whether a number is a perfect number or not using a user-defined
function.
def perfect(n):
s = 0
for i in
range(1, n):
if n % i
== 0:
s =
s + i
if s == n
and n > 0:
return
"Perfect number"
else:
return
"Not a perfect number"
n = int(input("Enter a number: "))
print(f"{n} is {perfect(n)}.")
Program 96: Write a Python program to
calculate the sum of even and odd numbers separately up to n using a
user-defined function.
def calculate(n):
even = 0
odd = 0
for i in
range(1, n + 1):
if i % 2
== 0:
even
= even + i
else:
odd
= odd + i
return even,
odd
n = int(input("Enter a number: "))
e, o = calculate(n)
print("Sum of even numbers =", e)
print("Sum of odd numbers =", o)
Program 97: Write a Python program to
display all prime numbers up to a given number using a user-defined function.
def prime(n):
for num in
range(2, n + 1):
count =
0
for i in
range(1, num + 1):
if
num % i == 0:
count = count + 1
if count
== 2:
print(num, end=" ")
n = int(input("Enter a number: "))
print("Prime numbers are:")
prime(n)
Program 98: Write a Python program to
calculate the monthly electricity bill based on unit consumption using a
user-defined function.
Criteria:
- 0–20 units: Rs. 30
(Minimum charge)
- 21–45 units: Rs. 5 per
unit
- Above 45 units: Rs. 7
per unit
def electricity_bill(units):
if units
<= 20:
bill =
30
elif units
<= 45:
bill =
units * 5
else:
bill =
units * 7
return bill
units = int(input("Enter the monthly unit
consumption: "))
print("Total Electricity Bill = Rs.",
electricity_bill(units))
Program 99: Write a Python program to
calculate the delivery charge based on the distance using a user-defined
function.
Criteria:
- Up to 5 km: Rs. 50
- Above 5 km up to 10 km: Rs. 100
- Above 10 km: Rs. 150
def delivery_charge(distance):
if distance
<= 5:
charge =
50
elif
distance <= 10:
charge =
100
else:
charge =
150
return
charge
distance = float(input("Enter delivery distance
in km: "))
print("Delivery Charge = Rs.",
delivery_charge(distance))
Program 100: Write a Python program to
calculate the employee bonus based on monthly salary using a user-defined
function.
Criteria:
- Salary Rs. 70,000 or above: 25% bonus
- Salary Rs. 50,000 or above: 20% bonus
- Salary Rs. 40,000 or above: 15% bonus
- Salary Rs. 30,000 or above: 10% bonus
- Below Rs. 30,000: 5% bonus
def bonus(salary):
if salary
>= 70000:
b =
salary * 25 / 100
elif salary
>= 50000:
b =
salary * 20 / 100
elif salary
>= 40000:
b =
salary * 15 / 100
elif salary
>= 30000:
b =
salary * 10 / 100
else:
b =
salary * 5 / 100
return b
salary = float(input("Enter monthly salary:
"))
print("Employee Bonus = Rs.", bonus(salary))
No comments:
Post a Comment