Python: Division - Hacker Rank Solution


Python: Division - Hacker Rank Solution


Problem


Tutorial :
In Python, there are two kinds of division: integer division and float division.
Python 2 syntax
from __future__ import division

# floating point division
print 4 / 3

# integer division  
print 4 // 3

Python 3 syntax
print(4 / 3)
print(4 // 3)

Gives the output
1.3333333333333333 
1

Note: The __ in __future__ is a double underscore.
During the time of Python 2, when you divided one integer by another integer, no matter what, the result would always be an integer.
For example:
>>> 4/3 
1

In order to make this a float division, you would need to convert one of the arguments into a float.
For example:
>>> 4/3.0
1.3333333333333333

Since Python doesn't declare data types in advance, you never know when you want to use integers and when you want to use a float. Since floats lose precision, it's not advised to use them in integral calculations.
To solve this problem, future Python modules included a new type of division called integer division given by the operator //.
Now, / performs float division, and // performs integer division.
In Python 2, we will import a feature from the module __future__ called division.

Task :
Read two integers and print two lines. The first line should contain integer division, a//b . The second line should contain float division, a/b.
You don't need to perform any rounding or formatting operations.



Input Format :

The first line contains the first integer, a. The second line contains the second integer, b.

Output Format :

Print the two lines as described above.



Sample Input :

4
3

Sample Output :

1
1.33333333333



Solution :


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# Python: Division - Hacker Rank Solution
from __future__ import division

if __name__ == '__main__':
    a = int(raw_input())
    b = int(raw_input())
    # Python: Division - Hacker Rank Solution START 
    print(a//b);
    print(a/b);
    # Python: Division - Hacker Rank Solution END 





Disclaimer :-
the above hole problem statement is given by hackerrank.com but the solution is generated by the codeworld19 authority if any of the query regarding this post or website fill the following contact form thank you.

Next Post Previous Post
1 Comments
  • wndows10 systeem64 arabic
    wndows10 systeem64 arabic Sunday, August 15, 2021

    thank you

Add Comment
comment url