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)