Informatics Practices

WAP to perform all the mathematical operations of a calculator.

Python Control Flow

6 Likes

Answer

print("Select operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")

choice = input("Enter choice (1/2/3/4): ")

num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

if choice == '1':
    result = num1 + num2
    print(num1, "+", num2, "=", result)
elif choice == '2':
    result = num1 - num2
    print(num1, "-", num2, "=", result)
elif choice == '3':
    result = num1 * num2
    print(num1, "*", num2, "=", result)
elif choice == '4':
    if num2 != 0:
        result = num1 / num2
        print(num1, "/", num2, "=", result)
    else:
        print("Division by zero is not allowed.")
else:
    print("Invalid input")

Output

Select operation:
1. Add
2. Subtract
3. Multiply
4. Divide
Enter choice (1/2/3/4): 1
Enter first number: 23
Enter second number: 45
23.0 + 45.0 = 68.0
Select operation:
1. Add
2. Subtract
3. Multiply
4. Divide
Enter choice (1/2/3/4): 2
Enter first number: 48
Enter second number: 23
48.0 - 23.0 = 25.0
Select operation:
1. Add
2. Subtract
3. Multiply
4. Divide
Enter choice (1/2/3/4): 3
Enter first number: 3
Enter second number: 6
3.0 * 6.0 = 18.0
Select operation:
1. Add
2. Subtract
3. Multiply
4. Divide
Enter choice (1/2/3/4): 4
Enter first number: 245
Enter second number: 5
245.0 / 5.0 = 49.0

Answered By

1 Like


Related Questions