1Compute sum, subtraction, multiplication, division and exponent of given
variables input by the user.
# Get input from the user
a = float(input("Enter the first number: "))
b = float(input("Enter the second number: "))
# Perform calculations
print("Sum:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
# Check for division by zero
if b != 0:
print("Division:", a / b)
else:
print("Division: Cannot divide by zero.")
print("Exponentiation (a^b):", a ** b)
2. Compute area of following shapes: circle, rectangle, triangle, square,
trapezoid and parallelogram.
# Circle
radius = float(input("Enter radius of the circle: "))
pi = 3.14159
circle_area = pi * radius * radius
print("Circle Area:", circle_area)
# Rectangle
length = float(input("\nEnter length of the rectangle: "))
width = float(input("Enter width of the rectangle: "))
rectangle_area = length * width
print("Rectangle Area:", rectangle_area)
# Triangle
base = float(input("\nEnter base of the triangle: "))
height = float(input("Enter height of the triangle: "))
triangle_area = 0.5 * base * height
print("Triangle Area:", triangle_area)
# Square
side = float(input("\nEnter side of the square: "))
square_area = side * side
print("Square Area:", square_area)
# Trapezoid
base1 = float(input("\nEnter base1 of the trapezoid: "))
base2 = float(input("Enter base2 of the trapezoid: "))
height = float(input("Enter height of the trapezoid: "))
trapezoid_area = 0.5 * (base1 + base2) * height
print("Trapezoid Area:", trapezoid_area)
# Parallelogram
base = float(input("\nEnter base of the parallelogram: "))
height = float(input("Enter height of the parallelogram: "))
parallelogram_area = base * height
print("Parallelogram Area:", parallelogram_area)
3. Compute volume of following 3D shapes: cube, cylinder, cone and sphere.
# Cube
side = float(input("Enter side length of the cube: "))
cube_volume = side ** 3
print("Cube Volume:", cube_volume)
# Cylinder
radius = float(input("\nEnter radius of the cylinder: "))
height = float(input("Enter height of the cylinder: "))
pi = 3.14159
cylinder_volume = pi * radius ** 2 * height
print("Cylinder Volume:", cylinder_volume)
# Cone
radius = float(input("\nEnter radius of the cone: "))
height = float(input("Enter height of the cone: "))
cone_volume = (1/3) * pi * radius ** 2 * height
print("Cone Volume:", cone_volume)
# Sphere
radius = float(input("\nEnter radius of the sphere: "))
sphere_volume = (4/3) * pi * radius ** 3
print("Sphere Volume:", sphere_volume)
4 Compute and print roots of quadratic equation ax2+bx+c=0, where the values
of a, b, and c are input by the user.
import cmath # Supports complex number operations
# Get coefficients from the user
a = float(input("Enter coefficient a (not 0): "))
b = float(input("Enter coefficient b: "))
c = float(input("Enter coefficient c: "))
if a == 0:
print("This is not a quadratic equation.")
else:
# Calculate the discriminant
D = [Link](b**2 - 4*a*c)
# Compute two roots
root1 = (-b + D) / (2*a)
root2 = (-b - D) / (2*a)
# Print the results
print("Root 1:", root1)
print("Root 2:", root2)
5. Print numbers up to N which are not divisible by 3, 6, 9,, e.g., 1, 2, 4,
5, 7,….
# Get input from the user
N = int(input("Enter a number N: "))
print(f"Numbers up to {N} not divisible by 3, 6, or 9 are:")
for i in range(1, N + 1):
if i % 3 != 0 and i % 6 != 0 and i % 9 != 0:
print(i, end=" ")
[Link] a program to determine whether a triangle is isosceles or not?
# Get the lengths of the three sides from the user
side1 = float(input("Enter length of first side: "))
side2 = float(input("Enter length of second side: "))
side3 = float(input("Enter length of third side: "))
# Check if it forms a valid triangle first
if (side1 + side2 > side3) and (side2 + side3 > side1) and (side1 + side3 > side2):
# Check for isosceles
if side1 == side2 or side2 == side3 or side1 == side3:
print("The triangle is isosceles.")
else:
print("The triangle is not isosceles.")
else:
print("The given sides do not form a valid triangle.")
7. Print multiplication table of a number input by the user.
# Get input from the user
num = int(input("Enter a number to print its multiplication table: "))
# Print the table from 1 to 10
print(f"Multiplication table of {num}:")
for i in range(1, 11):
print(f"{num} x {i} = {num * i}")
8 Compute sum of natural numbers from one to n number.
# Get input from the user
n = int(input("Enter a positive integer: "))
# Validate input
if n < 1:
print("Please enter a number greater than or equal to 1.")
else:
# Use the formula for sum of first n natural numbers: n(n + 1)/2
total = n * (n + 1) // 2
print(f"The sum of natural numbers from 1 to {n} is {total}")
9. Print Fibonacci series up to n numbers e.g. 0 1 1 2 3 5 8 13…..n
# Get input from the user
n = int(input("Enter the number of Fibonacci terms to print: "))
# Starting values
a, b = 0, 1
count = 0
# Print Fibonacci sequence
print("Fibonacci sequence:")
while count < n:
print(a, end=" ")
a, b = b, a + b
count += 1
[Link] factorial of a given number.
# Get user input
n = int(input("Enter a non-negative integer: "))
# Validate input
if n < 0:
print("Factorial is not defined for negative numbers.")
else:
factorial = 1
for i in range(1, n + 1):
factorial *= i
print(f"The factorial of {n} is {factorial}")