Python Codes | Codes to make calculater | Codes to make Tables | Code to find factorial of numbers | Computer Science | CS
CODE 1: TO MAKE A CALCULATER IN PYTHON
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
print("select operation")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
while True:
choice=input("Enter choice (1/2/3/4): ")
if choice in ('1','2','3','4'):
num1=float(input("enter first number: "))
num2=float(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))
break
else:
print("Invalid Input")
______________________________________________
CODE 2: TO MAKE A CALCULATER IN PYTHON {2}
result=0
val1=float(input("Enter value 1; "))
val2=float(input("Enter value 2; "))
op = input("Enter any one of the operator (+,-,*,/); " )
if op == "+":
result= val1 + val2
elif op=="-":
if val1 > val2:
result= val1 - val2
else:
result= val2 - val1
elif op=="*":
result= val1 * val2
elif op=="/":
if val2 == 0:
print("Error! Division by zero is not allowed. Program terminated")
else:
result= val1/val2
else:
print("Wrong input, program terminated")
print("The result is ",result)
Comments
Post a Comment