- Related Questions & Answers
- C Program for Tower of Hanoi
- C++ Program for Dijkstra’s shortest path algorithm?
- C/C++ Program for nth Catalan Number?
- C Program to Multiply two Floating Point Numbers?
- C program to find the length of a string?
- C Program to Check if all digits of a number divide it
- C Program to Check if a Given String is a Palindrome?
- Write a program to Delete a Tree in C programming
- Write a C program to print “ Tutorials Point ” without using a semicolon
- Sum of the nodes of a Singly Linked List in C Program
- Sum of first n natural numbers in C Program
- C Program for simple interest?
- Area of a n-sided regular polygon with given Radius in C Program?
- Area of a leaf inside a square in C Program?
- C Program for Area of a square inscribed in a circle which is inscribed in a hexagon?

In this program we will see how to accomplish the basic calculator functionalities of a calculator using a python program. Here we create individual functions to carry out the calculations and return the result. Also user input is accepted along with the choice of operator.

# This function performs additiion def add(a, b): return a + b # This function performs subtraction def subtract(a, b): return a - b # This function performs multiplication def multiply(a, b): return a * b # This function performs division def divide(a, b): return a / b print("Select an operation.") print("+") print("-") print("*") print("/") # User input choice = input("Enter operator to use:") A = int(input("Enter first number: ")) B = int(input("Enter second number: ")) if choice == '+': print(A,"+",B,"=", add(A,B)) elif choice == '-': print(A,"-",B,"=", subtract(A,B)) elif choice == '*': print(A,"*",B,"=", multiply(A,B)) elif choice == '/': print(A,"/",B,"=", divide(A,B)) else: print("Invalid input")

Running the above code gives us the following result −

Select an operation. + - * / Enter operator to use: - Enter first number: 34 Enter second number: 20 34 - 20 = 14

Advertisements