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.

<?php
  echo "Enter first number: ";
  $number1 = trim(fgets(STDIN));

  echo "Enter second number: ";
  $number2 = trim(fgets(STDIN));
  
  $q = intval($number1 / $number2);
  $r = $number1 % $number2;
  
  echo "Integer Quotient: ", $q, "\nInteger Remainder: ", $r;
?>

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.

public static void main(String[] args) throws java.io.IOException {
  java.io.BufferedReader cin = new java.io.
          BufferedReader(new java.io.InputStreamReader(System.in));
  int number1, number2, q, r;

  System.out.print("Enter first number: ");
  number1 = Integer.parseInt(cin.readLine());

  System.out.print("Enter second number: ");
  number2 = Integer.parseInt(cin.readLine());
  
  q = (int)(number1 / number2);
  r = number1 % number2;
  
  System.out.println("Integer Quotient: " + q + "\nInteger Remainder: " + r);
}

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.

#include <iostream>
using namespace std;
int main() {
  int number1, number2, q, r;

  cout << "Enter first number: ";
  cin >> number1;
  cout << "Enter second number: ";
  cin >> number2;
  
  q = (int)(number1 / number2);
  r = number1 % number2;
  cout << "Integer Quotient: " << q << "\nInteger Remainder: " << r;
  return 0;
}

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.

static void Main() {
  int number1, number2, q, r;

  Console.Write("Enter first number: ");
  number1 = Int32.Parse(Console.ReadLine());

  Console.Write("Enter second number: ");
  number2 = Int32.Parse(Console.ReadLine());
  
  q = (int)(number1 / number2);
  r = number1 % number2;
  
  Console.Write("Integer Quotient: " + q + "\nInteger Remainder: " + r);
  Console.ReadKey();
}

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.

Sub Main()
  Dim number1, number2, q, r As Integer

  Console.Write("Enter first number: ")
  number1 = Console.ReadLine()

  Console.Write("Enter second number: ")
  number2 = Console.ReadLine()

  q = number1 \ number2
  r = number1 Mod number2

  Console.Write("Integer Quotient: " & q & vbCrLf & "Integer Remainder: " & r)

  Console.ReadKey()
End Sub

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.

number1 = int(input("Enter first number: "))
number2 = int(input("Enter second number: "))

q = number1 // number2
r = number1 % number2

print("Integer Quotient:", q, "\nInteger Remainder:", r)

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

number1 = int(input("Enter first number: "))
number2 = int(input("Enter second number: "))

q, r = divmod(number1, number2)

print("Integer Quotient:", q, "\nInteger Remainder:", r)

2 Responses to Calculating the Quotient and Remainder of Integer Division

  • Hello. In my class we cannot use divmod. Is there another way to find the quotient and remainder by only an operator

    I mean, instead of /, %

    • To find x DIV y and x MOD y without using any of the /, // or % operators one possible solution would be to subtract y from x repeatedly until no more subtractions can be performed. In Python it could be something like the following:

      count = 0
      while x - y > 0:
          count = count + 1
          x = x - y
      
      print(count) #This is the quotient
      print(x)     #This is the remainder
      

      Now, if you wish to use just ONE arithmetic operator, you can do something like the following:

      count = 0
      while x - y > 0:
          count = count - (-1)
          x = x - y
      
      print(count) #This is the quotient
      print(x)     #This is the remainder
      

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!