In this article you will learn how to accept two integer numbers from the user using a Python program and then perform the arithmetic operations on those two integers.
let us try to solve this using the Python program.
Write A Python Script To Read Two Integer Numbers And Perform Arithmetic Operations
Take integer variables named as first and second using the input function call, ask the user to enter integer numbers and then convert it into an integer using the type conversion.
first = int(input("Enter First Number :"))
second = int(input("Enter Second Number :"))
Now using the print statement, print the addition, subtraction and product of two numbers as shown below.
print(f"Addition of Two Numbers {first} + {second} = {first + second}")
print(f"Subtraction of Two Numbers {first} - {second} = {first - second}")
print(f"Product of Two Numbers {first} * {second} = {first * second}")
Now in order to perform the division of 2 integer numbers we will be using a try and exception block to catch the zero division error. lets use try block and except block to perform the division of two numbers and print the result
Similarly let's perform the exponential arithmetic operation by using the double star operator “ ** ” to get the power of x raised to y.
try:
print(f"Division of Two Numbers {first} / {second} = {first / second}")
except:
print("Division by zero is not possible")
print(f"Exponentials of Two Numbers {first} ^ {second} = {first ** second}")
Python Script
=======================
first = int(input("Enter First Number :"))
second = int(input("Enter Second Number :"))
print(f"Addition of Two Numbers {first} + {second} = {first + second}")
print(f"Subtraction of Two Numbers {first} - {second} = {first - second}")
print(f"Product of Two Numbers {first} * {second} = {first * second}")
try:
print(f"Division of Two Numbers {first} / {second} = {first / second}")
except:
print("Division by zero is not possible")
print(f"Exponentials of Two Numbers {first} ^ {second} = {first ** second}")
Output
==============
Enter First Number :23
Enter Second Number :7
Addition of Two Numbers 23 + 7 = 30
Subtraction of Two Numbers 23 - 7 = 16
Product of Two Numbers 23 * 7 = 161
Division of Two Numbers 23 / 7 = 3.2857142857142856
Exponentials of Two Numbers 23 ^ 7 = 3404825447
Output
==============
Enter First Number :55
Enter Second Number :11
Addition of Two Numbers 55 + 11 = 66
Subtraction of Two Numbers 55 - 11 = 44
Product of Two Numbers 55 * 11 = 605
Division of Two Numbers 55 / 11 = 5.0
Exponentials of Two Numbers 55 ^ 11 = 13931233916552734375
Output
==============
Enter First Number :78
Enter Second Number :0
Addition of Two Numbers 78 + 0 = 78
Subtraction of Two Numbers 78 - 0 = 78
Product of Two Numbers 78 * 0 = 0
Division by zero is not possible
Exponentials of Two Numbers 78 ^ 0 = 1
Output
==============
Enter First Number :0
Enter Second Number :99
Addition of Two Numbers 0 + 99 = 99
Subtraction of Two Numbers 0 - 99 = -99
Product of Two Numbers 0 * 99 = 0
Division of Two Numbers 0 / 99 = 0.0
Exponentials of Two Numbers 0 ^ 99 = 0
Conclusion
==================
Execute the above Python program by providing to random integer values as a input to the Python program and not down the Arithmetic operation results
Comment it down below if you have any queries related to above Python program
0 Comments