To make simple calculator in python to perform basic mathematical operations such as add, subtract, multiply, and divide two numbers entered by the user. To make calculator in python, first provide 5 options to the user, the fifth option for exit. After providing all the five options to the user, ask from user to enter his/her choice and perform the desired operation as shown in the program given below.
Following python program provides options to the user and ask to enter his/her choice to perform and show the desired result as output:
# Python Program - Make Simple Calculator print("1. Addition"); print("2. Subtraction"); print("3. Multiplication"); print("4. Division"); print("5. Exit"); choice = int(input("Enter your choice: ")); if (choice>=1 and choice<=4): print("Enter two numbers: "); num1 = int(input()); num2 = int(input()); if choice == 1: res = num1 + num2; print("Result = ", res); elif choice == 2: res = num1 - num2; print("Result = ", res); elif choice == 3: res = num1 * num2; print("Result = ", res); else: res = num1 / num2; print("Result = ", res); elif choice == 5: exit(); else: print("Wrong input..!!");
Here are some sample runs of the above python program to illustrates how to make a simple calculator.
This is the initial output giving users, some number of choices (5 choices) to choose any one from all these 5 choices.
Now let's first try for the addition task by choosing or typing 1 as input and then enter any two number say 1 and 2 and then press enter key to check it out:
Now let's re-run the calculator program to this time check it out for subtraction of two numbers say 2 and 1 as shown in the following sample run:
Again re-run the same program, and this time, provide 4 as input to check for division task and supply two numbers, that is first number as 20 and second numbers as 3 then press enter key as shown here in the below sample run:
Here is the same program written and run on python shell. This is the first snapshot:
Below is the second snapshot:
Here is the third snapshot:
You may also like to learn or practice the same program in other popular programming languages: