Practical No.
1: A program to calculate simple interest (I); the principal
amount (P), the rate of interest(r) and the deposited time (t) in years are
manually entered.
Answer:
Python Code:
P = float(input("Enter the principal amount (P): "))
r = float(input("Enter the annual rate of interest (r in %): "))
t = float(input("Enter the time (t in years): "))
I = (P * r * t) / 100
print(f"\nSimple Interest (I) = {I:.2f}")
Output:
Enter the principal amount (P): 1234
Enter the annual rate of interest (r in %): 12.34
Enter the time (t in years): 12
Simple Interest (I) = 1827.31
Practical No. 2: A program to calculate Compound interest (I); the principal
amount (P), the rate of interest(r) and the deposited time (t) in years are
manually entered.
Answer:
Python Code:
P = float(input("Enter the principal amount (P): "))
r = float(input("Enter the annual interest rate in % (r): "))
t = float(input("Enter the time in years (t): "))
A = P * (1 + r / 100) ** t
I=A-P
print(f"\nCompound Interest (I) = {I:.2f}")
print(f"Total Amount (A) after {t} years = {A:.2f}")
Output:
Enter the principal amount (P): 1234
Enter the annual interest rate in % (r): 12.34
Enter the time in years (t): 12
Compound Interest (I) = 3751.72
Total Amount (A) after 12.0 years = 4985.72
Practical No. 3: A program that takes temperature in Celsius as input,
converts it to Fahrenheit and prints the result on the screen. It then checks
the values for set data of Celsius temperatures.
Answer:
Python Code:
def celsius_to_fahrenheit(c):
return (9/5) * c + 32
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = celsius_to_fahrenheit(celsius)
print(f"Temperature in Fahrenheit: {fahrenheit:.2f} °F")
celsius_list = [-20, -10, 0, 10, 20, 30, 40, 50, 100]
print("\nTemperature Conversion Table (Celsius to Fahrenheit):")
print("Celsius (°C) | Fahrenheit (°F)")
for c in celsius_list:
f = celsius_to_fahrenheit(c)
print(f"{c:>12} | {f:>14.2f}")
Output:
Enter temperature in Celsius: 39.45
Temperature in Fahrenheit: 103.01 °F
Temperature Conversion Table (Celsius to Fahrenheit):
Celsius (°C)| Fahrenheit (°F)
-20 | -4.00
-10 | 14.00
0| 32.00
10 | 50.00
20 | 68.00
30 | 86.00
40 | 104.00
50 | 122.00
100 | 212.00
Practical No. 4: A program to calculate the sine value for a given angle in
degrees.
Answer:
Python Code:
import math
def calculate_sine(degrees):
radians = [Link](degrees)
return [Link](radians)
try:
angle = float(input("Enter an angle in degrees: "))
result = calculate_sine(angle)
print(f"\nThe value of sin({angle}°) is {result:.4f}")
except ValueError:
print("Invalid input. Please enter a numeric value.")
Output:
Enter an angle in degrees: 43.78
The value of sin(43.78°) is 0.6919
Practical No. 5: A program that takes two integers as input and prints the
LCM of two numbers on the screen. Also checking two different data set.
Answer:
P.T.O
Python Code:
def compute_lcm(a, b):
greater = max(a, b)
while True:
if greater % a == 0 and greater % b == 0:
return greater
greater += 1
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
lcm = compute_lcm(num1, num2)
print(f"\nThe LCM of {num1} and {num2} is {lcm}")
print("\nLCM of two different data set:")
test1_a, test1_b = 12, 18
test1_lcm = compute_lcm(test1_a, test1_b)
print(f"LCM of {test1_a} and {test1_b} is {test1_lcm}")
test2_a, test2_b = 7, 5
test2_lcm = compute_lcm(test2_a, test2_b)
print(f"LCM of {test2_a} and {test2_b} is {test2_lcm}")
Output:
Enter first number: 1
Enter second number: 2 The LCM of 1 and 2 is 2
LCM of two different data set:
LCM of 12 and 18 is 36
LCM of 7 and 5 is 35
Practical No. 6: A program that takes an integer (base 10) as input then
converts it to Binary number (base 2) and print on the screen.
Answer:
Python Code:
n = int(input("Enter a decimal number: "))
binary = ""
if n == 0:
binary = "0"
else:
while n > 0:
r=n%2
binary = str(r) + binary
n = n // 2
print("The Binary number is:", binary)
Output:
Enter a decimal number: 45
The Binary number is: 101101
Practical No. 7: A program that takes the length of the three sides of a
triangle as inputs and prints the area on the screen.
Answer:
Python Code:
a = float(input("Enter length of side a: "))
b = float(input("Enter length of side b: "))
c = float(input("Enter length of side c: "))
if a + b > c and b + c > a and c + a > b:
s = (a + b + c) / 2
area = (s * (s - a) * (s - b) * (s - c)) ** 0.5
print(f"\nArea of the triangle = {round(area, 2)} square units")
else:
print("\nThe given sides do not form a valid triangle.")
Output:
Enter length of side a: 7
Enter length of side b: 10
Enter length of side c: 5
Area of the triangle = 16.25 square units
Practical No. 8: A program that takes two objects masses and the distance
between them as input and determines the gravitational force acting
between them. (G = 6.673×10^-11 N.m²/kg²)
Answer:
Python Code:
G = 6.674 * (10 ** -11)
m1 = float(input("Enter mass of first object (in kg): "))
m2 = float(input("Enter mass of second object (in kg): "))
r = float(input("Enter distance between the objects (in meters): "))
if r == 0:
print("Distance cannot be zero.")
elif r < 0:
print("Distance cannot be negative.")
else:
F = G * (m1 * m2) / (r ** 2)
print(f"\nThe Gravitational Force is {F:.4e} N")
Output:
Enter mass of first object (in kg): 52
Enter mass of second object (in kg): 71
Enter distance between the objects (in meters): 55
The Gravitational Force is 8.1456e-11 N
Practical
No. 9: A program to calculate the distance between two points represented
by (x1, y1) and (x2, y2). Checking the result with three different set of
points.
Answer:
Python Code:
def distance(x1, y1, x2, y2):
return ((x2 - x1)**2 + (y2 - y1)**2) ** 0.5
for i in range(1, 4):
print(f"\nSet {i}:")
x1 = float(input("Enter x1: "))
y1 = float(input("Enter y1: "))
x2 = float(input("Enter x2: "))
y2 = float(input("Enter y2: "))
d = distance(x1, y1, x2, y2)
print(f"Distance between ({x1}, {y1}) and ({x2}, {y2}) = {round(d, 2)}")
Output:
Set 1:
Enter x1: 1
Enter y1: 2
Enter x2: 3
Enter y2: 4
Distance between (1.0, 2.0) and (3.0, 4.0) = 2.83
Set 2:
Enter x1: 5
Enter y1: 6
Enter x2: 7
Enter y2: 8
Distance between (5.0, 6.0) and (7.0, 8.0) = 2.83
Set 3:
Enter x1: 9
Enter y1: 1
Enter x2: 2
Enter y2: 3
Distance between (9.0, 1.0) and (2.0, 3.0) = 7.28
Practical No. 10: A program that takes an integer (N) as input and find the
sum of first N Natural Numbers. Checking the result at least three different
integers and printing it.
Answer:
Python Code:
for i in range(1, 4):
N = int(input(f"Enter natural number {i}: "))
if N < 1:
print("Please enter a number greater than 0.\n")
continue
total = 0
for num in range(1, N + 1):
total += num
print(f"Sum of first {N} natural numbers is: {total}\n")
Output:
Enter natural number 1: 41
Sum of first 41 natural numbers is: 861
Enter natural number 2: 23
Sum of first 23 natural numbers is: 276
Enter natural number 3: 32
Sum of first 32 natural numbers is: 528
Practical No. 11: A program to accept two matrices as inputs and find their
sum.
Answer:
Python Code:
rows = int(input("Enter number of rows: "))
cols = int(input("Enter number of columns: "))
print("\nEnter elements of Matrix A:")
A = []
for i in range(rows):
row = []
for j in range(cols):
value = int(input(f"Enter A[{i+1}][{j+1}]: "))
[Link](value)
[Link](row)
print("\nEnter elements of Matrix B:")
B = []
for i in range(rows):
row = []
for j in range(cols):
value = int(input(f"Enter B[{i+1}][{j+1}]: "))
[Link](value)
[Link](row)
Sum = []
for i in range(rows):
row = []
for j in range(cols):
[Link](A[i][j] + B[i][j])
[Link](row)
print("\nMatrix A:")
for row in A:
print(*row)
print("\nMatrix B:")
for row in B:
print(*row)
print("\nSum of A and B:")
for row in Sum:
print(*row)
Output:
Enter number of rows: 2
Enter number of columns: 2
Enter elements of Matrix A:
Enter A[1][1]: 1
Enter A[1][2]: 2
Enter A[2][1]: 3
Enter A[2][2]: 4
Enter elements of Matrix B:
Enter B[1][1]: 5
Enter B[1][2]: 6
Enter B[2][1]: 7
Enter B[2][2]: 8
Matrix A:
12
34
Matrix B:
56
78
Sum of A and B:
68
10 12
Practical No. 12: A program to create a list of t (in years) within the range of
[1, 25] and the graphical representation of Simple Interest vs Time (Years)
using matplotlib module. Principal amount (P) and rate of interest (r) are
manually entered.
Answer:
Python Code:
import [Link] as plt
# Input from user
P = float(input("Enter the principal amount (P): ")) r=
float(input("Enter the annual rate of interest (r in %): "))
# Create list of time values from 1 to 25
time_years = list(range(1, 26))
# simple interest for each year
simple_interests = [(P * r * t) / 100 for t in time_years
# Plotting [Link](figsize=(7, 5))
[Link](time_years, simple_interests, marker='o', linestyle='-', color='b')
[Link]("Simple Interest vs Time (Years)", fontsize=14) [Link]("Time (t) in
Years", fontsize=12) [Link]("Simple Interest (I)", fontsize=12)
[Link](True) [Link](time_years)
plt.tight_layout()
# Display the graph [Link]()
Output:
Enter the principal amount (P): 1234 Enter the
annual rate of interest (r in %): 12.34
Graph Output:
Practical No. 13: A program to create an array of t (in years) within the
range of [1, 25] and give the graphical representation of the Compound
Interest Growth Over Time using matplotlib module. Principal amount (P)
and rate of interest (r) are manually entered.
Answer:
Python Code:
import [Link] as plt import numpy as np
# Get input from user P = float(input("Enter the
principal amount (P): ")) r = float(input("Enter the annual interest
rate in % (r): "))
# Create an array of time in years from 1 to 25 t=
[Link](1, 26)
# Calculate amount for each t using compound interest formula A=
P * (1 + r / 100) ** t
# Plotting the graph [Link](figsize=(7, 5))
[Link](t, A, marker='o', linestyle='-', color='blue', label='Total Amount (A)’)
# Graph customization [Link]("Compound Interest Growth
Over Time") [Link]("Time (t) in years $ $")
[Link]("Total Amount (A) $ $") [Link](True)
[Link]() [Link](t) # Show all year points from
1 to 20 plt.tight_layout()
# Show the plot [Link]()
Output:
Enter the principal amount (P): 1234 Enter the annual
interest rate in % (r): 12.34
Graph Output:
Practical No. 14: A program to graphical representation ten different Celsius
values into Fahrenheit using matplotlib.
Answer:
Python Code:
import [Link] as plt
# Function to convert Celsius to Fahrenheit
def celsius_to_fahrenheit(c): return (9/5) * c + 32
# List of 10 different Celsius values celsius_list = [-10, 0, 5,
10, 15, 20, 25, 30, 35, 40]
# Convert each Celsius to Fahrenheit fahrenheit_list =
[celsius_to_fahrenheit(c) for c in celsius_list]
# Print conversion table print("Celsius to Fahrenheit
Conversion:") print("Celsius (°C) | Fahrenheit (°F)")
print("-------------------------------") for c, f in zip(celsius_list,
fahrenheit_list): print(f"{c:>12} | {f:>14.2f}")
# Plotting the conversion [Link](figsize=(7, 5))
[Link](celsius_list, fahrenheit_list, marker='o', color='red', linestyle='-')
[Link]("Temperature Conversion: Celsius to Fahrenheit", fontsize=14) [Link]("Celsius
(°C)") [Link]("Fahrenheit (°F)")
[Link](True) [Link](celsius_list)
plt.tight_layout()
# Show the plot [Link]()
Output:
Celsius to Fahrenheit Conversion: Celsius (°C) |
Fahrenheit (°F) -------------------------------
-10 | 14.00
0| 32.00
5| 41.00
10 | 50.00
15 | 59.00
20 | 68.00
25 | 77.00
30 | 86.00
35 | 95.00
40 | 104.00
Graph Output:
Practical No. 15: A program that takes two objects masses as input and
create a list of distance r (in meter) within the range of [10, 1000] and plot a
graph of Gravitational Force vs Distance. (G = 6.673×10^-11 N.m^2/kg^2).
Answer:
Python Code:
import numpy as np import [Link] as
plt
# Constants G = 6.674 * (10 ** -11) #
Gravitational constant
# Masses (can be customized) m1 = float(input("Enter
mass of first object (in kg): ")) m2 = float(input("Enter mass of
second object (in kg): "))
# Create distance array from 10 to 1000 meters r=
[Link](10, 1000, 500) # 500 points for smooth curve
# Calculate gravitational force for each distance F = G * m1 * m2 /
(r ** 2)
# Plotting [Link](figsize=(7, 5))
[Link](r, F, label='Gravitational Force', color='c') [Link]('Gravitational Force vs
Distance') [Link]('Distance (m)') [Link]('Force
(N)') [Link](True)
[Link]() plt.tight_layout()
# Show the plot [Link]()
Output:
Enter mass of first object (in kg): 52 Enter mass of
second object (in kg): 71
Graph Output:
Practical No. 16: A program to create an array x within the range of [-10, 10]
and a graph plot of sin(x) vs x using matplotlib module.
Answer:
Python Code:
import numpy as np
import [Link] as plt
x = [Link](-10, 10, 250) # 250 points between -10 and 10 y=
[Link](x) # Compute sin(x)
# Plot sin(x) vs x [Link](figsize=(5, 4))
[Link](x, y, label='sin(x)', color='blue') [Link]('Plot of
sin(x) vs x') [Link]('$x $')
[Link]('$sin(x) $') [Link](True)
[Link]() [Link](0, color='black',
linewidth=0.5) # x-axis [Link](0, color='black', linewidth=0.5) # y-axis
plt.tight_layout
# Show the plot [Link]()
Graph Output:
Practical No. 17: A program to create a list of x in range of [-5, 5] and a
graph plot of e^{-x} vs x using matplotlib module.
Answer:
Python Code:
import numpy as np import
[Link] as plt
# Create x values from -5 to 5 x = [Link](-5, 5,
200) y = [Link](-x)
# Plotting [Link](figsize=(5, 4))
[Link](x, y, label=r'$e^{-x}$', color='purple') [Link]('Graph of $e^{-x}
$ vs x') [Link]('$x $')
[Link](r'$e^{-x} $') [Link](True)
[Link]() plt.tight_layout()
# Show the plot [Link]()
Graph Output:
Practical 18: A program to create a list of x within the range of [2, 4] and a
graph plot of the function f(x) = x3 - 3x + 1 using matplotlib module.
Answer:
Python Code:
import [Link] as plt
# Create list of x values in the range [2, 4] x = [i * 0.1 for i in
range(20, 41)] # step of 0.1 y = [xi**3 - 3*xi + 1 for xi in x]
# f(x) = x^3 - 3x + 1
# Plot the function [Link](figsize=(5, 4))
[Link](x, y, color='purple', label='${f(x) = x^3 - 3x + 1}$') [Link]("Plot of
f(x) = ${x^3 - 3x + 1}$") [Link]('$x $')
[Link]('$f(x) $') [Link](True)
[Link]() plt.tight_layout()
# Show the plot [Link]()
Graph Output:
Practical No. 19: A program to create a list of x within the range of [-1, 1]
and a graph plot of the function y = ax - bx^2 using matplotlib module. With
Set data a = 0.25 and b = 0.5.
Answer:
Python Code:
import numpy as np import
[Link] as plt
# Constants a = 0.25
b = 0.5
# Create list of x values from -1 to 1
x = [Link](-1, 1, 200)
# Calculate y = ax - bx^2 y=a*x-
b * x**2
# Plotting [Link](figsize=(5, 4))
[Link](x, y, label='${y = 0.25x - 0.5x^2}$', color='blue')
[Link]('Plot of ${y = ax - bx^2}$') [Link]('$x $')
[Link]('$y $') [Link](True)
[Link]() plt.tight_layout()
# Show the plot [Link]()
Graph Output:
Practical No. 20: A program to create a list of t within the range of [0, 20]
and use matplotlib to plot the exponential decay function y = y_0 e^{-k t}
with initial value y_0 = 10 and decay constant k = 0.5.
Answer:
Python Code:
import numpy as np import
[Link] as plt
# Constants y0 = 10 # Initial
value k = 0.5 # Decay constant
# Create list of t from 0 to 20 (with 100 points for smooth curve)
t = [Link](0, 20, 100) y = y0 *
[Link](-k * t)
# Plotting [Link](t, y, label=r'$y =
y_0 e^{-kt}$', color='green') [Link]('Exponential Decay: $y =
y_0 e^{-kt}$') [Link]('Time (t)')
[Link]('y') [Link](True)
[Link]() [Link]()
Graph Output: