C++ program to find Quotient and Remainder

Write a C++ program that takes two integers, a dividend and a divisor, from the user and calculates the quotient and the remainder of their division.

Input and Output:

  1. Input: Dividend = 10, Divisor = 3 Output: Quotient = 3, Remainder = 1
  2. Input: Dividend = 25, Divisor = 4 Output: Quotient = 6, Remainder = 1

Algorithm

  1. Prompt the user to enter the dividend and divisor.
  2. Ensure the divisor is not zero to prevent division by zero errors.
  3. Calculate the quotient using the division operator.
  4. Calculate the remainder using the modulus operator.
  5. Display the results (quotient and remainder) to the user.

Here is the C++ code that follows the algorithm to calculate and print the quotient and remainder:

cpp
#include<iostream>
using namespace std;

int main() {
    int dividend, divisor, quotient, remainder;

    // Step 1: Get input from the user
    cout << "Enter dividend: ";
    cin >> dividend;
    cout << "Enter divisor: ";
    cin >> divisor;

    // Step 2: Check if divisor is zero
    if (divisor == 0) {
        cout << "Error! Divisor cannot be zero.";
        return 1; // Exit the program with an error code
    }

    // Step 3: Calculate quotient
    quotient = dividend / divisor;

    // Step 4: Calculate remainder
    remainder = dividend % divisor;

    // Step 5: Display the results
    cout << "Quotient = " << quotient << endl;
    cout << "Remainder = " << remainder << endl;

    return 0; // Successful completion of the program
}

Variations with Different Input Values

The program is designed to handle all integer values, including negatives. Here's how it works with different types of inputs:

Input and Output Examples for the Modified Program:

  1. Input: Dividend = -15, Divisor = 4 Output: Quotient = -3, Remainder = -3
  2. Input: Dividend = 20, Divisor = -5 Output: Quotient = -4, Remainder = 0
tools

Programming

Related Articles