C++ Program to Compute Quotient and Remainder

In this post, you’ll learn how to write a Program to Compute Quotient and Remainder in C++.

This lesson will help you learn how to find the Quotient and Reminder using the Division function, one of the basic Mathematical functions using the C++ language. Let’s look at the below source code.

How to Compute Quotient and Remainder in C++?

RUN CODE SNIPPET

Source Code

#include <iostream>
using namespace std;
int main() 
{
   int divisor, dividend; 
   int quotient, remainder;
   dividend = 15;
   divisor = 7;
   quotient = dividend / divisor;
   remainder = dividend % divisor;
   cout << "Dividend is: " << dividend <<endl;
   cout << "Divisor is: " << divisor <<endl;
   cout << "Quotient is: " << quotient << endl;
   cout << "Remainder is: " << remainder;
   return 0;
}

Output

Dividend is: 15
Divisor is: 7
Quotient is: 2
Remainder is: 1

Let’s break down the code and elaborate the steps, to understand better

#include <iostream>

  • This line which is called the header file is used in every C++ codding. #include statement tells the compiler to use available files and <iostream>is the name of the specific that we have used in this code.
  • The <iostream> file stands for Input and Output statement. This file includes the output statement ‘cout’, ‘cin’, and various other statements.

using namespace std;

  • The C++ has a standard library that has files for different functions and this line is used to access the standard file for input and output statements.

int main();

  • This line usually controls the function of the code, as it calls the functions to perform their tasks.
  • The int main()shows that the input value is a type of integer, once the program is executed the function returns to the main function, by using the statement ‘return 0;’.

{ }

  • The opening ‘ { ‘ and the closing ‘ } ‘ curly braces mark the start and the finish of the main function
  • Every statement and value between these braces belong to the main function.

The above statements are the main factors that support the function of the source code. Now we can look into the working and layout of the code’s function.

  1. First declare the characters (divisor, dividend, quotient, remainder) as integers and assign the values to be calculated –  dividend = 15; divisor = 7;.
  2. Next display the output statements with the cout and the Insertion Operator ‘ << ‘.
  3. When using sentences, write them within quotes ” “, " Quotient is: " followed by ‘ << ‘ and the integer, to display them.
  4.  Now we can include the statement to perform the Mathematical function – Division. Using the division operator ( / ) we can find the Quotient and to find the Reminder we use the () operator. The answers are stored in the ‘Quotient’ and the ‘Reminder’ characters.
  5. Using the output statement cout and the respective characters, to display the answer.
  6. End the function with return 0; this returns the function to main( ).

Now that you have understood how the source code performs its function, try it yourself with different values to it understand better.

Note: The ‘ << endl ‘ in the code is used to end the current line and move to the next line, to understand how it works exclude it from the code, move it around and work with it.

Additional Source Code

You can also try the source code given below, the following code is structured as to receive the values from the user rather than include the values directly in the code.

RUN CODE SNIPPET
#include <iostream>
using namespace std;
int main()
{    
    int divisor, dividend;
    int quotient, remainder;
    cin >> dividend;
    cout << "Enter dividend: "<< dividend <<endl;
    cin >> divisor;
    cout << "Enter divisor: "<< divisor <<endl;
    quotient = dividend / divisor;
    remainder = dividend % divisor;
    cout << "Quotient: " << quotient << endl;
    cout << "Remainder: " << remainder;
    return 0;
}

Input

25
11

Output

Enter dividend: 25
Enter divisor: 11
Quotient: 2
Remainder: 3
  • The structure is similar to the previous code, the extra function we include is the cin >> dividend; and cin >> divisor; , this statement is used to collect and store the values that the user enters in the assigned integer.
  • We use << dividend << divisor << quotient << remainder to display the stored values in the output.
  • The rest of the statements and functions are similar, to perform the Compute Quotient and Remainder.

Share:

Leave A Reply

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

You May Also Like

In this post, you will learn how to Generate Multiplication Table using C++ programming language. This lesson will teach you...
  • C++
  • January 30, 2022
In this post, you will learn how to Display Fibonacci Sequence using C++ programming language. This lesson will teach you...
  • C++
  • January 30, 2022
In this post, you will learn how to Find GCD of two Numbers using C++ programming language. This lesson will...
  • C++
  • January 30, 2022