The Provided Code Stub Reads Two Integers And From Stdin. Add Logic to Print Two Lines. The First Line Should Contain The Result of Integer Division //. The Second Line Should Contain The Result of Float Division / . no Rounding or Formatting is Necessary

Hi in this article you will learn a python script that takes integers as input,  displays the result of integer division and floating point division.

I will also be using a try and accept block as we are performing the division Operation.

 

 

The Provided Code Stub Reads Two Integers, And , From Stdin. Add Logic to Print Two Lines. The First Line Should Contain The Result of Integer Division, // . The Second Line Should Contain The Result of Float Division, / . no Rounding or Formatting is Necessary




 

The Provided Code Stub Reads Two Integers, And , From Stdin. Add Logic to Print Two Lines. The First Line Should Contain The Result of Integer Division, // . The Second Line Should Contain The Result of Float Division, / . no Rounding or Formatting is Necessary

I will be taking two integer variables A and B and asking a user to enter the two integers using the input function call and then convert it into an integer.

a = int(input("Enter First Number : "))
b = int(input("Enter Second Number : "))


 



Then use the try block to  perform that true division of the first number divided by the second,  also print to print the true division of two given numbers.  

Now use another print statement to perform the division of two numbers.  so this will be a normal division where the first number is divided by the second number.

try:
   print(f"True division of {a} // {b} is : {a // b}")
   print(f"Normal division of {a} / {b} is : {a / b}")
except:
   print(f"Zero Division Error : Division is Not Possible")


Using the except block to catch a zero division exception if the second number is 0. Inside the exception block 

 



Complete Python Program
===============================

a = int(input("Enter First Number : "))
b = int(input("Enter Second Number : "))

try:
   print(f"True division of {a} // {b} is : {a // b}")
   print(f"Normal division of {a} / {b} is : {a / b}")
except:
   print(f"Zero Division Error : Division is Not Possible")




Complete Python Program another version
==================================


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

try:
   print(f"True division of {first} // {second} is : {first // second}")
   print(f"Normal division of {first} / {second} is : {first / second}")
except:
   print(f"Zero Division Error : Division is Not Possible as denominator is Zero ")




Output 1
======================

Enter First Number : 13
Enter Second Number : 9
True division of 13 // 9 is : 1
Normal division of 13 / 9 is : 1.4444444444444444


Output 2
======================

Enter First Number : 34
Enter Second Number : 3
True division of 34 // 3 is : 11
Normal division of 34 / 3 is : 11.333333333333334



Output 3
===================

Enter First Number : 12
Enter Second Number : 4
True division of 12 // 4 is : 3
Normal division of 12 / 4 is : 3.0




Output 4
=============

Enter First Number : 4
Enter Second Number : 0
Zero Division Error : Division is Not Possible as denominator is Zero



Conclusion
==================

The above Python program uses input function call and then try except block to  perform the two division and normal division.

Execute the above Python program by yourself providing random  numbers as input to the python script.

Comment it down below if you have any queries related to the above Python program.



Post a Comment

0 Comments