We are going to create only a simple calculator that can perform addition, subtraction, multiplication and division. You can also return the result from the functions and print it out inside the caller class.
All of these functions take two numbers as parameter. 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.
The loop will run an infinite amount of time until the user stops it. Addition" ; print "2. If the input is not valid, the program is printing one message asking the user to enter a valid input.
In this example, we are printing out the result inside each function. This is the first snapshot: Below is the second snapshot: Here is the third snapshot: Same Program in Other Languages You may also like to learn or practice the same program in other popular programming languages:.
Just add more functions for each type of calculations and add more if-elif conditions. Inside the loop, we are using multiple if-elif-else cases.
We are using one while loop to create this infinite loop. For each calculation add, subtract, multiply, divide we have different functions.