How to get Quotient and Remainder using C++
In this part of the short tutorial series, we are going to show you how to get the quotient and remainder in C++.
As you execute the program, it asks you to enter the dividend and divisor values (3rd program). Then we will divide the two numbers and get the remainder and quotient and display both values.
See the C++ programs below.
C++ program to get remainder
In this C++ program, we will get the remainder after dividing two numbers. For that, we used the modulus operator (% sign).
#include <iostream> using namespace std; int main() { int divisor = 10; int dividend = 54; int remainder; //Getting remainder remainder = dividend % divisor; cout << "The Remainder of 54/10 = " << remainder <<"\n\n"; return 0; }
Result:
Get the quotient example
In order to get the remainder after the division of two numbers, use the forward slash / (division). The example below calculates and displays the remainder:
#include <iostream> using namespace std; int main() { int divisor = 10; int dividend = 54; int quotient; //Getting quotient quotient = dividend / divisor; cout << "The Quotient of 54/10 = " << quotient <<"\n\n"; return 0; }
Result:
A simple program that takes user input and gets both – remainder and quotient
In this program, the user is asked to enter two numbers for division. Then we used slash (/) and modulus (%) to get the remainder and quotient. All values are displayed i.e.
- Dividend
- Divisor
- Remainder
- Quotient
#include <iostream> using namespace std; int main() { int divisor, dividend; int quotient, remainder; cout << "Please enter the dividend: "; cin >> dividend; cout << "Please enter the divisor: "; cin >> divisor; //Getting quotient quotient = dividend / divisor; //Getting remainder remainder = dividend % divisor; //Displaying all values cout << "The Dividend = " << dividend <<"\n"; cout << "The Divisor = " << divisor <<"\n"; cout << "The Quotient = " << quotient <<"\n"; cout << "The Remainder = " << remainder <<"\n\n"; return 0; }
Sample output:
Sample output as dividend is negative:
Sample output as the divisor is negative