Math Functions : Beginner's Level
In this blog post, we will see how can we create simple Mathematics functions by using python programming Language, so here we go...
#You can add comments in the below code and share it in the comment box :)
#comments make the code easy to understand
def add(a,b):
return(a+b)
def sub(a,b):
return(a-b)
def div(a,b):
if a == b:
return 1
elif b == 0:
return ("Math Error: division by zero")
else:
return(float(a//b))
def mult(a,b):
return(a*b)
def power(a,b):
c = 1
for p in range(0,b):
c *= a
return(c)
def main():
print("-------------------------------------------")
print("The available functions are the following: ")
print("1-Addition\n2-Subtraction\n3-Division\n4-Multiplication\n5-Power")
counter = 0
while(counter < 20):
print("-------------------------------------------")
print("")
option = input("Enter the number from 1-5 corresponding to the above functions: ")
if option == 1:
print("So, you want to perform Addition" )
num1 = int(input("Please enter any number: "))
num2 = int(input("Please enter another number: "))
print("The addition of " + str(num1) + " and " + str(num2) + " = " )
print(add(num1,num2))
elif option == 2:
print("So, you want to perform Subtraction" )
num1 = int(input("Please enter any number: "))
num2 = int(input("Please enter another number: "))
print("The subtraction of " + str(num1) + " and " + str(num2) + " = ")
print(sub(num1,num2))
elif option == 3:
print("So, you want to perform Division" )
num1 = int(input("Please enter any number: "))
num2 = int(input("Please enter another number: "))
print("The division of " + str(num1) + " and " + str(num2) + " = ")
print(div(num1,num2))
elif option == 4:
print("So, you want to perform Multiplication" )
num1 = int(input("Please enter any number: "))
num2 = int(input("Please enter another number: "))
print("The multiplication of " + str(num1) + " and " + str(num2) + " = ")
print(mult(num1,num2))
elif option == 5:
print("So, you want to take Power" )
num1 = int(input("Please enter any number: "))
num2 = int(input("Please enter another number: "))
print("The power of " + str(num1) + " to " + str(num2) + " = ")
print(power(num1,num2))
else:
print("Invalid Input!")
counter = counter + 1
main()
Comments
Post a Comment