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:

  1. Addition
  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")
print("Your answer is: ",result)

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 (+, -, *, /, %): +
Your answer is: 10

Analysis of Calculator Program in Python

As far as the time complexity is concerned, no such iterations are made with five conditions to perform operations on the two operands taken as input, the time complexity of the program remains constant. Hence Time Complexity can be denoted as O(1).

For space complexity, no extra auxiliary space is consumed with the only storage used being for a couple of integer operands and an operation string. Thus, the space complexity for the calculator program is constant as well. The Space Complexity can be denoted as O(1).

Conclusion
In this article, we studied the working of a calculator program, with further sections of this article on calculator programs in python explaining dry run, algorithm, code, time and space complexity of calculator program in python.

We expect you liked this article on calculator program in python. Hope to see you again at PrepBytes with another informative and insightful article.

Other Python Programs
Python program to reverse a number
Python program for heap sort
Python program to check armstrong number
Python program to check leap year
Python program to convert celsius to fahrenheit
Python program to find factorial of a number
Python program to reverse a linked list
Python Program to find the middle of a linked list using only one traversal
Python Program to Add Two Numbers
Python Program to Check Palindrome Number
Python Program to Print the Fibonacci Series
Python Loop Program
Anagram Program in Python
Fizzbuzz Program in Python
String Programs in Python
List Program in Python
Prime Number Program in Python
Exception Handling in Python
Python Program to Reverse a String

Leave a Reply

Your email address will not be published. Required fields are marked *