Write A Python Program To Enter Two Integers And Perform All Arithmetic Operations On Them

In this article let's learn a Python program that asks the user to enter two integers and then performs arithmetic operations like addition, subtraction, multiplication, division modulus and exponential on these two  integer numbers.

Let's perform all the five arithmetic operations using two numbers.

 

Write A Python Program To Enter Two Integers And Perform All Arithmetic Operations On Them




Write A Python Program To Enter Two Integers And Perform All Arithmetic Operations On Them

Let's take two variables first and second to store the integer,  using the input function call, ask the user to enter the first and second integer then convert the  user entered value to integer using the type conversion as shown below.

first = int(input("Enter First Integer : "))
second = int(input("Enter Second Integer : "))


Let's use the print statement to print the addition, subtraction and product  of two numbers using the print formatting.  just use the print statement  directly with the help of formatting to perform addition  subtraction and multiplication as shown below

print(f"\nAddition of {first} + {second} is : {first + second}")
print(f"Subtraction of {first} - {second} is : {first - second}")
print(f"Product of {first} x {second} is : {first * second}")




In order to try the division of two numbers you should know that the denominator should not be zero so use the try and exception block  to divide two numbers.  if denominator is zero catch the zero division error exception as shown below. Perform modulus as well in the same try block.


try:
   print(f"Division of {first} / {second} is : {first / second}")
   print(f"Modulus of {first} % {second} is : {first % second}")
except:
   print("Division by Zero is not possible")




Once we complete the division and modulus of two numbers,  using the print statement again try to raise the second number  exponential  of the first number as shown below,  use the double star operator ** to perform the exponential operation

print(f"Exponential of {first} ^ {second} is : {first ** second}")




Python Code
===================

first = int(input("Enter First Integer : "))
second = int(input("Enter Second Integer : "))

print(f"\nAddition of {first} + {second} is : {first + second}")
print(f"Subtraction of {first} - {second} is : {first - second}")
print(f"Product of {first} x {second} is : {first * second}")

try:
   print(f"Division of {first} / {second} is : {first / second}")
   print(f"Modulus of {first} % {second} is : {first % second}")
except:
   print("Division by Zero is not possible")

print(f"Exponential of {first} ^ {second} is : {first ** second}")






OUTPUT
===============
Enter First Integer : 12
Enter Second Integer : 2

Addition of 12 + 2 is : 14
Subtraction of 12 - 2 is : 10
Product of 12 x 2 is : 24
Division of 12 / 2 is : 6.0
Modulus of 12 % 2 is : 0
Exponential of 12 ^ 2 is : 144




OUTPUT
==============
Enter First Integer : 3
Enter Second Integer : 0

Addition of 3 + 0 is : 3
Subtraction of 3 - 0 is : 3
Product of 3 x 0 is : 0
Division by Zero is not possible
Exponential of 3 ^ 0 is : 1




OUTPUT
==============
Enter First Integer : 20
Enter Second Integer : 3

Addition of 20 + 3 is : 23
Subtraction of 20 - 3 is : 17
Product of 20 x 3 is : 60
Division of 20 / 3 is : 6.666666666666667
Modulus of 20 % 3 is : 2
Exponential of 20 ^ 3 is : 8000




Conclusion
====================
Execute the above program by providing two integer inputs to the  Python program,  note down the results.

Comment down below if you have any suggestions to improve the above Python program



Post a Comment

0 Comments