Mod Divmod HackerRank Solution In Python

Problem

One of the built-in functions of Python is divmod, which takes two arguments a and b and returns a tuple containing the quotient of a/ b first and then the remainder a.

For Example:

>>> print divmod(177,10)(17, 7)

Here, the integer division is 177/10 => 17 and the modulo operator is 177%10 => 7.

Task

Read in two integers,  and , and print three lines.
The first line is the integer division a/b (While using Python2 remember to import division from __future__).
The second line is the result of the modulo operator: a%b.
The third line prints the divmod of a and b.

Input Format

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

Output Format

Print the result as described above.

Sample Input

17710

Sample Output

177(17, 7)

Solution to HackerRank Mod Divmod in Python

Aim

In this problem, we are to find the division of two numbers printing out the characteristic and the mantissa on different lines and together in a tuple.

a = int(input())b = int(input())print(a//b)print(a%b)print(divmod(a, b))

Note: The divmod statement in the last line is a Python math function that prints out the characteristic and Mantissa of a division in a tuple. Learn more on divmod function.

Alternative Solution

This solution solves the question without using any math function.

a = int(input())b = int(input())print(a//b)print(a%b)print((a//b, a%b))

Thanks.

Comments