Table of Contents
C++ Program to Compute Quotient and Remainder
In this tutorial, we will discuss the C++ Program to Compute Quotient and Remainder
In this post, we are going to learn how to find quotient and remainder using two numbers via different 3 ways in C++language
Program to Compute Quotient and Remainder
Compute Quotient and Remainder – standard method
Program 1
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int dividend=400,divisor=30;
int quotient,remainder;
quotient=dividend/divisor;// /operator use to compute quotient
remainder=dividend%divisor;// %operator use to compute remainder
cout<<"Quotient = "<<quotient;
cout<<"\nRemainder = "<<remainder;
getch();
return 0;
}
When the above code is executed, it produces the following result
Quotient = 13 Remainder = 10
In this program,
- integer variables dividend,divisor are declared and initialized
- integer variables quotient,remainder are declared.
- The dividend and divisor both are used to calculate quotient and remainder
- Then,the program is displayed the output of using cout<<
Compute Quotient and Remainder – Using user input
Program 2
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int dividend,divisor;
int quotient,remainder;
cout<<"Enter dividend: " ;
cin>>dividend;
cout<<"Enter divisor: " ;
cin>>divisor;
quotient=dividend/divisor; // / operator use to compute quotient
remainder=dividend%divisor; // % operator use to compute quotient
cout<<"Quotient = "<<quotient;
cout<<"\nRemainder = "<<remainder;
getch();
return 0;
}
When the above code is executed, it produces the following result
Enter dividend: 245 Enter divisor: 8 Quotient = 30 Remainder = 5
Approach
- integer variables dividend and divisor are declared,
- integer variables quotient and remainder are declared.
- The program asks input from the user
- Then the user enters the input values for dividend and divisor.
- The program will read the input using cin>> and store the variables dividend and divisor respectively
- The dividend and divisor both are used to calculate quotient and remainder.
- Then,the program is displayed the output of using cout<<
Compute Quotient and Remainder – Using function
Program 3
#include <iostream>
#include <conio.h>
using namespace std;
//user define function for find quotient
void findQuotient(int a, int b){
int Quotient= a/b;
cout<<"Quotient =" <<Quotient;
}
//user define function for find remainder
void findRemainder(int a, int b){
int Remainder =a%b;
cout<<"\nRemainder "<<Remainder;
}
int main()
{
int dividend=100,divisor=5;
//integer variable declaration
findQuotient(dividend,divisor);
//Call the function for display quotient
findRemainder(dividend,divisor);
//Call the function for display remainder
getch();
return 0;
}
When the above code is executed, it produces the following result
Quotient = 20
Remainder = 0
Program 4
#include <iostream>
#include <conio.h>
using namespace std;
int findQuitient(int a, int b){
return a/b;
}
int findRemainder(int a, int b){
return a%b;
}
int main()
{
int dividend,divisor;
cout<<"Enter dividend: " ;
cin>>dividend;
cout<<"Enter divisor: " ;
cin>>divisor;
cout<<"Quotient = "<<findQuitient(dividend,divisor);
cout<<"\nRemainder "<<findRemainder(dividend,divisor);
getch();
return 0;
}
When the above code is executed, it produces the following result
Enter dividend: 500 Enter divisor: 55 Quotient = 9 Remainder = 5
Approach
- Declare dividend and divisor.
- The program takes input from the user
- Then the user enters the input value for dividend and divisor.
- the program will read the input using cin>> and store the variables in dividend and divisor respectively
- define the function findQuotient() and findRemainder() to find quotient and remainder
- Call the function to produce output
- finally, the program displays the output using cout<< function
Program 5
#include <iostream>
#include <conio.h>
using namespace std;
//user define function for find quotient
void findQuotient(int a, int b){
int Quotient= a/b;
cout<<"Quotient =" <<Quotient;
}
//user define function for find remainder
void findRemainder(int a, int b){
int Remainder =a%b;
cout<<"\nRemainder "<<Remainder;
}
int main()
{
int dividend,divisor;
//integer variable declaration
cout<<"Enter dividend: " ;
//Ask input from the user for dividend
cin>>dividend;
//Reading the input
cout<<"Enter divisor: " ;
//Ask input from the user for divisor
cin>>divisor;
//Reading the input
findQuotient(dividend,divisor);
//Call the function for display quotient
findRemainder(dividend,divisor);
//Call the function for display remainder
getch();
return 0;
}
When the above code is executed, it produces the following result
Enter dividend: 240 Enter divisor: 25 Quotient = 9 Remainder = 15
Suggested for you
Data type in C++ language
Variable in C++ language
Similar post
Python Program to Compute Quotient and Remainder