What is calculator?
A calculator may be a device that performs arithmetic operations on numbers. The only the best calculators will do only addition, subtraction, multiplication, and division. Additional subtle calculators will handle exponential operations, roots, log s, pure mathematics functions, and hyperbolic functions. Internally, some calculators really perform all of those functions by continual processes of addition.
What is function in python ?
A operate may be a block of code that solely runs once it's known as. you'll be able to pass information, called parameters, into a operate. A operate will come information as a result.
Lets start with calculator programming in python?
First step: I create one function and function name is add with x and y arguments
def add(x,y):
return x+y
Second step : I create one function and function name is sub x and y arguments
def sub(x,y):
return x-y
Third step : I create one function and function name is mul x and y arguments
def mul(x,y):
return x*y
Fourth step : I create one function and function name is div x and y arguments
def div(x,y):
return x/y
Fifth step : I select operation as per my requirement using print statement
print(“ ----- SELECT OPERATION-----”)
print(“ ----- PRESS 1 FOR ADDITION-----”)
print(“ ----- PRESS 2 FOR MULTIPLICATION-----”)
print(“ ----- PRESS 3 FOR SUBTRACTION-----”)
print(“ ----- PRESS 4 FOR DIVISION-----”)
Sixth step : I run while loop till when condition is true
while True:
I take the input from my side and take select name variable and take input
select= input(“ enter choice(1/2/3/4):”)
Seventh step :Take the numeric digit in form of input using a and b variable name
if choice in(‘1’,’2’,’3’,’4’):
a= float(input(“enter the first input”)
b=float(input(“enter the second input”)
Eight step : I run if elif else condition for selecting the operation
if select == ‘1’:
print(a, “+”, b, ”=”, add(a,b))
elif select == ‘2’:
print(a, “*”, b, ”=”, mul(a,b))
elif select == ‘3’:
print(a, “-”, b, ”=”, sub(a,b))
elif select == ‘4’:
print(a, “/”, b, ”=”, div(a,b))
break
else:
print(“ERROR”)
Final output : Run the program
----- SELECT OPERATION-----
----- PRESS 1 FOR ADDITION -----
----- PRESS 2 FOR MULTIPLICATION -----
----- PRESS 3 FOR SUBTRACTION -----
----- PRESS 4 FOR DIVISION -----
ENTER MY CHOICE – 3
ENTER THE FIRST INPUT – 12
ENTER THE SECOND INPUT - 9
12*9= 108
Author
Amazing knowledge
ReplyDelete