Hi Guys, welcome to my blog in this article you will learn about how to Write A Program To Perform Different Arithmetic Operations On Numbers In Python
Title :
Write A Program To Perform Different Arithmetic Operations On Numbers In Python
Description :
Hi in this article You will learn a Python program to perform the automatic operations. The arithmetic operations that we are going to perform in the below program are addition subtraction multiplication division and exponential.
Take to variables num1 and num2 using the input function call asking user to enter two floating point numbers
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
Once we get the two floating point variables, use the print function call to print the addition, subtraction multiplication and exponential of these two floating point numbers.
print(f"Addition of {num1} + {num2} is : {num1 + num2} ")
print(f"Subtraction of {num1} - {num2} is : {num1 - num2} ")
print(f"Multiplication of {num1} * {num2} is : {num1 * num2} ")
print(f"Exponential of {num1} ^ {num2} is : {num1 ** num2} ")
Now using the try and except block try the operation of division, num1 divided by the variable num2 and use the print function call to print the division result. Use the except block to catch the zero division error and print the appropriate error message using print function call.
try:
print(f"Division of {num1} / {num2} is : {num1 / num2} ")
except ZeroDivisionError:
print("Cannot divide by zero")
Complete Python Code :
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
print(f"Addition of {num1} + {num2} is : {num1 + num2} ")
print(f"Subtraction of {num1} - {num2} is : {num1 - num2} ")
print(f"Multiplication of {num1} * {num2} is : {num1 * num2} ")
print(f"Exponential of {num1} ^ {num2} is : {num1 ** num2} ")
try:
print(f"Division of {num1} / {num2} is : {num1 / num2} ")
except ZeroDivisionError:
print("Cannot divide by zero")
Output :
Enter first number: 100
Enter second number: 20
Result of division is : 5.00
Output :
Enter first number: 23
Enter second number: 0
Error: Cannot divide by zero
Conclusion :
The above Python program asks the user to enter two numbers either integer or floating point values.
once the user enters the two numbers so the arithmetic operations performed are addition, subtraction, multiplication, division and exponential
Upon performing the arithmetic operations the results are printed onto a standard output.
Comment it down below if you have any suggestions to improve the above Python program
0 Comments