0% found this document useful (0 votes)
2 views2 pages

Python Program 3

The document outlines a simple calculator program that performs basic arithmetic operations such as subtraction, multiplication, division, floor division, modulus, and exponentiation. It also includes functions to calculate the area of various shapes: rectangle, square, circle, and triangle, with input validation for numeric values. The program runs in a loop, allowing users to select a shape or exit the program.

Uploaded by

jananikhavya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Python Program 3

The document outlines a simple calculator program that performs basic arithmetic operations such as subtraction, multiplication, division, floor division, modulus, and exponentiation. It also includes functions to calculate the area of various shapes: rectangle, square, circle, and triangle, with input validation for numeric values. The program runs in a loop, allowing users to select a shape or exit the program.

Uploaded by

jananikhavya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

print(f"Subtraction ({num1} - {num2}) = {num1 - num2}")

# 4. Multiplication (*)
print(f"Multiplication ({num1} * {num2}) = {num1 * num2}")

# 5. True Division (/)


if num2 != 0:
print(f"Division ({num1} / {num2}:) = {num1 / num2:.2f}")
else:
print("Division (/) = Error: Cannot divide by zero")

# 6. Floor Division (//) - Rounds down to the nearest whole integer


if num2 != 0:
print(f"Floor Division ({num1} // {num2}) = {num1 // num2}")
else:
print("Floor Division (//) = Error: Cannot divide by zero")
# 7. Modulus (%) - Returns the remainder of division
if num2 != 0:
print(f"Modulus/Remainder ({num1} % {num2}) = {num1 % num2}")
else:
print("Modulus (%) = Error: Cannot calculate remainder when dividing by zero")
# 8. Exponentiation (**) - Raises num1 to the power of num2
print(f"Exponentiation ({num1} ** {num2}) = {num1 ** num2}")
—----------------------------------------------------------------------------------------------------
3. import math
def get_valid_number(prompt):
while True:
try:
num=float(input(prompt))
return num
except ValueError:
print("Invalid Input. Enter only numeric values..")

def calculate_rectangle_area():
length = get_valid_number("Enter length: ")
width = get_valid_number("Enter width: "))
area = length * width
print(f"Area of Rectangle: {area}\n")

def calculate_square_area():
side = get_valid_number("Enter side length: "))
area = side ** 2
print(f"Area of Square: {area}\n")

def calculate_circle_area():
radius = get_valid_number("Enter radius: "))
area = [Link] * (radius ** 2)
print(f"Area of Circle: {area:.4f}\n")
def calculate_triangle_area():
base = get_valid_number("Enter base: "))
height = get_valid_number("Enter height: "))
area = 0.5 * base * height
print(f"Area of Triangle: {area}\n")

while True:
print("--- AREA CALCULATOR ---")
print("1. Rectangle")
print("2. Square")
print("3. Circle")
print("4. Triangle")
print("5. Exit")

choice = input("Select a shape (1-5): ")

if choice == '1':
calculate_rectangle_area()
elif choice == '2':
calculate_square_area()
elif choice == '3':
calculate_circle_area()
elif choice == '4':
calculate_triangle_area()
elif choice == '5':
print("Exiting program. Goodbye!")
break
else:
print("Invalid choice. Please select from 1-4, or 5 to exit.\n")

You might also like