-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7.py
More file actions
61 lines (48 loc) · 1.77 KB
/
7.py
File metadata and controls
61 lines (48 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
Program No. : 7
#Program : Combine all above 5 arithmetic operations in one program.
def add(a, b):
"""Calculates the sum of two numbers."""
return a + b
def subtract(a, b):
"""Calculates the difference of two numbers."""
return a - b
def multiply(a, b):
"""Calculates the product of two numbers."""
return a * b
def divide(a, b):
"""Calculates the quotient of two numbers, with division-by-zero handling."""
if b == 0:
return "Cannot divide by zero (Denominator is 0)"
else:
# Standard division, resulting in a float
return a / b
def average(a, b):
"""Calculates the mean (average) of two numbers."""
# Sum of numbers divided by the count (which is 2)
return (a + b) / 2
# --- Main Program Execution ---
print("--- Basic Arithmetic Calculator ---")
# 1. Get user input
try:
num1_str = input("Enter the first number (A): ")
num2_str = input("Enter the second number (B): ")
# Convert inputs to float to handle decimals in all operations
num1 = float(num1_str)
num2 = float(num2_str)
except ValueError:
print("Error: Invalid input. Please enter valid numbers.")
exit() # Stop the program if input is invalid
# 2. Perform all operations
sum_result = add(num1, num2)
difference_result = subtract(num1, num2)
product_result = multiply(num1, num2)
division_result = divide(num1, num2)
average_result = average(num1, num2)
# 3. Display all results
print("\n--- Results ---")
print(f"Numbers used: A = {num1}, B = {num2}\n")
print(f"1. Addition (A + B): {sum_result}")
print(f"2. Subtraction (A - B): {difference_result}")
print(f"3. Multiplication (A * B): {product_result}")
print(f"4. Division (A / B): {division_result}")
print(f"5. Average of A and B: {average_result}")