Python Program - Simple Calculator



Simple Calculator Program

Here is a simple Python program to create a calculator that can perform basic arithmetic operations like addition [1], subtraction, multiplication, or division.


Objective of This Program

The objective of this program is to create a simple calculator that can perform basic arithmetic operations such as addition [1], subtraction, multiplication, and division.


Pseudocode for This Program

1. Define the four arithmetic functions:

    1.1 add(x, y) - returns the sum of x and y.

    1.2 subtract(x, y) - returns the difference of x and y.

    1.3 multiply(x, y) - returns the product of x and y.

    1.4 divide(x, y) - returns the quotient of x and y.


2. Print a menu of available operations to the user:

    2.1 "Select operation."

    2.2 "1. Add"

    2.3 "2. Subtract"

    2.4 "3. Multiply"

    2.5 "4. Divide"


3. Take the user's choice of operation as input:

    3.1 Read the user's input into the variable `choice`.


4. Take the first number as input:

    4.1 Read the user's input into the variable `num1`.

    4.2 Convert `num1` from string to integer.


5. Take the second number as input:

    5.1 Read the user's input into the variable `num2`.

    5.2 Convert `num2` from string to integer.


6. Perform the selected operation and print the result:

    6.1 If `choice` is "1", call the `add` function with `num1` and `num2` as arguments, and print the result.

    6.2 If `choice` is "2", call the `subtract` function with `num1` and `num2` as arguments, and print the result.

    6.3 If `choice` is "3", call the `multiply` function with `num1` and `num2` as arguments, and print the result.

    6.4 If `choice` is "4", call the `divide` function with `num1` and `num2` as arguments, and print the result.

    6.5 If `choice` is none of the above, print an error message "Invalid input".


Program Source Code



# Create a calculator

# Define functions
def add(x, y):
   return x + y

def subtract(x, y):
   return x - y

def multiply(x, y):
   return x * y

def divide(x, y):
   return x / y

# Take input from the user
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")

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

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

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")



Console Output 



In this example, the user selected option 3 (Multiply) and entered 5 as the first number and 7 as the second number. The program then performed the multiplication operation and displayed the result, which is 35. 


Image by 200 Degrees from Pixabay

Comments

Popular posts from this blog

The New ChatGPT Reason Feature: What It Is and Why You Should Use It

Raspberry Pi Connect vs. RealVNC: A Comprehensive Comparison

The Reasoning Chain in DeepSeek R1: A Glimpse into AI’s Thought Process