# Calculator Program in Python In this article, we will study one of the most intuitive and use logical concepts by programming the Calculator. This article focuses on the working and features of the calculator, where we will be discussing the dry run, the algorithm and the code for the calculator program in python. The time and space complexity of the code will be discussed before binding up with the article.

By the end of the article, the reader is going to understand and sound enough to implement various mathematical operations on a couple of operands to get the resultant output.

## Calculator Program in Python

A calculator program can be put to use to perform arithmetic operations like addition, subtraction, division, multiplication and modulo of two numbers by getting user input and giving the output as the result of the computation.

The method discussed in this article will be using simple if-else conditions to print the result output on the basis of operation selected by the user as input.

## Dry Run of Calculator Program in Python

Suppose we have two operands at our disposal, p and q, and we have to perform one of the following arithmetic operation discussed, namely, as follow:

2. Subtraction
3. Multiplication
4. Division
5. Modulo (Remainder)

hence we assign 5 to a while 3 to b, tracing out for each operation,

Addition can be stated as, a+b which results in 5+3 = 8.

Subtraction can be stated as, a-b which results in 5-3 = 2.

Multiplication can be stated as, ab which results in 53 = 12.

Division can be stated as, a/b which results in 5//3 = 1. The division in python with two slash characters means that integer division is being performed as a single slash would return float value.

Modulo can be stated as, a%b which results in 5%3 = 2. It reacts as a remainder finding operator in Python with its support extending to most of the programming languages.

As suggested above, any of the operations will be performed, with the result stored in a variable. On printing the result, the program is terminated successfully.

## Algorithm for Calculator Program in Python

Now that we are already through with a dry run example on all of the operations to perform for a calculator program in python which might have made it easier for you to understand its algorithm. Here is a short and concise algorithm learning up to the implementation of code for the calculator program in python.

1. Start the program

2. Take two numbers as user input using the input function

3. Take the operation to be performed using the input function

4. Use conditional logic, if-else to select the operation to perform on the two operands.

5. Store the computed value in an object named result.

6. Print the result.

7. End the program.

## Code for Calculator Program in Python

Since we are through with the algorithm and dry run part, let us look at the calculator program in python. The link to the code is attached below along with the explanation.

### Approach- Using If-Else Method

```p = int(input("Enter the first input: "))
q = int(input("Enter the second input: "))

oper = input("Enter the type of operation you want to perform (+, -, *, /, %): ")

result = 0
if oper == "+":
result = p+q
elif oper == "-":
result = p-q
elif oper == "*":
result = p*q
elif oper == "/":
result = p//q # Integer Division
elif oper == "%":
result = p%q
else:
print("Invalid Input")

Explanation: In the above linked code, the first operand named p, taken as input proceeded with taking the second operand, named q, taken as input in the next line. The following line asks for the operation to perform. Once all the inputs are collected, conditional if-else check for each operation matching with the input, as soon as operator is matched with the input, the operation is performed to store the computed value in an object named result. The result is printed at the end, in case of incorrect input, Invalid Input is presented on screen.

Output:

``````Enter the first number: 3
Enter the second number: 7
Enter the type of operation you want to perform (+, -, *, /, %): +