Create a simple calculator which can perform basic arithmetic operations like addition, subtraction, multiplication or division depending upon the user input.
Here is a simple Python program to make a basic calculator that can perform addition, subtraction, multiplication, and division:
# define a function for addition def add(x, y): return x + y # define a function for subtraction def subtract(x, y): return x - y # define a function for multiplication def multiply(x, y): return x * y # define a function for division def divide(x, y): return x / y # take user input for operation choice print("Select operation.") print("1. Add") print("2. Subtract") print("3. Multiply") print("4. Divide") choice = input("Enter choice (1/2/3/4): ") # take user input for numbers num1 = int(input("Enter first number: ")) num2 = int(input("Enter second number: ")) # perform operation based on user choice if choice == '1': print(num1, "+", num2, "=", add(num1,num2)) elif choice == '2': print(num1, "-", num2, "=", subtract(num1,num2)) elif choice == '3': print(num1, "*", num2, "=", multiply(num1,num2)) elif choice == '4': print(num1, "/", num2, "=", divide(num1,num2)) else: print("Invalid input")
In this program, we define four functions for addition, subtraction, multiplication, and division respectively. Then, we take user input for the operation choice and the two numbers to perform the operation on. Finally, based on the user choice, we call the appropriate function to perform the operation and display the result.
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.