Write a program that prompts the user to enter two integers and then calculates the quotient and the remainder of the integer division.

Solution

PHP

Since PHP doesn’t actually incorporate an arithmetic operator that calculates the integer quotient, you can use the intval() function to achieve the same result.

Java

Since Java doesn’t actually incorporate an arithmetic operator that calculates the integer quotient, you can use the (int) casting operator to achieve the same result.

C++

Since C++ doesn’t actually incorporate an arithmetic operator that calculates the integer quotient, you can use the (int) casting operator to achieve the same result.

C#

Since C# doesn’t actually incorporate an arithmetic operator that calculates the integer quotient, you can use the (int) casting operator to achieve the same result.

Visual Basic

You can use the ( \ ) and the ( Mod ) operators of Visual Basic. The former performs an integer division and returns the integer quotient whereas the latter performs an integer division and returns the integer remainder. The solution is presented here.

Python

You can use the ( // ) and the ( % ) operators of Python. The former performs an integer division and returns the integer quotient whereas the latter performs an integer division and returns the integer remainder. The solution is presented here.

A more “Pythonic” way is to use the divmod() function as shown here.

Leave a Reply

Your email address will not be published. Required fields are marked *

This article is taken from a book of mine. If you enjoyed it, please visit my author's page inand check out all of my books!