Division in C++
C++ Division Arithmetic Operation
In C++ the arithmetic operator / is used for division. This operator takes two operands and returns the result of dividing the left operand by the right operand Using sample programs; in this tutorial you will learn how to use the arithmetic division operator with values ??of various data types.
Syntax
result = operand_1 / operand_2
Attempts to split operand_1 into equal parts, operand_2. The dividend is operand_1 and the divisor is operand_2.
The precision may or may not be stored in the result, depending on the data types of the operands andresult.
C++ Division by Two Integers
#include <iostream>
using namespace std;
int main() {
int a = 16;
int b = 7;
int div = a / b;
cout<< div <<endl;
}
The division operator can divide two integers. The following code snippet shows the data types of the operands and the return value. Since both operands are integers, if the dividend is not exactly divisible by the divisor, the division operator only returns the quotient and discards memory. The following program initializes two integer variables and then uses them as operands of a division operator.
Output:
2
C++ Division Using Two Floating Point Numbers
You can divide two floating point numbers using the division operator. The following code snippet shows the data types of the operands and the return value. The division operator divides the dividend by the divisor until the full floating-point precisionis reached because both the dividend and the divisor are floating-point numbers.
#include <iostream>
using namespace std;
int main() {
float a = 21.3;
float b = 4.1;
float div = a / b;
cout<< div <<endl;
}
Output:
5.19512
C++ Division by Integers and Floats
You can use integers for floating-point division. The following code snippet shows the data types of the operands and the return value. float = int/floatThe following program initializes an integer and a float variable, then divides and stores the float variable. Accuracy is preserved. However, precision is lost when the result is stored as an integer.
#include <iostream>
using namespace std;
int main() {
int a = 16;
float b = 7.1;
float div = a / b;
cout<< div <<endl;
}
Chaining Division Operators
To divide three or more operands in one statement, you can chain division operators. The fake code is shown below
- Result = Operand_1 / Operand_2 / Operand_3 /.. / operand_n
The following example uses the arithmetic division operator to divide all four integer variables in one statement. Calculations are performed from left to right. So the first step is to do operand_1 / operand_2 and divide the result.
#include <iostream>
using namespace std;
int main() {
int a = 100;
int b = 2;
int c = 10;
int div = a / b / c;
cout<< div <<endl;
}
Explicit Typecasting
Programmers do this explicitly. The static_cast keyword is used for explicit type casting.
Such a conversion is performed at compile time.
The explicit cast solves theinteger division problem in the following code. The m and n values ??are statically converted to a floating point in the code below on line 6; Therefore the result of the division is also a floating point number
#include<iostream>
using namespace std;
int main() {
int m = 10;
int n = 7;
float a = static_cast<float>(m)/ static_cast<float>(n); //static explicit casting
cout<< "The answer after explicit typecasting is: " <<a<<endl;
}
Output:
The answer after explicit typecasting is: 1.42857
Implicit Typecasting
The compiler automatically performs an implicit typecast. The expression is evaluated by the compiler based on the data types used in the expression
Alldata types used in a given expression are evaluated to the highest data type by the compiler.
The fundamental reason for theinteger divide function is the implicit cast. The calculation is implicitly converted to data type int when using two integer operands with arithmetic division, which truncates the decimal part of the result
As shown in the following code, we can change the type of a division operand to a floating point type to solve the integer division problem:
#include<iostream>
using namespace std;
int main() {
float a = 10.0/7;
cout<< "The answer after implicit typecasting is: " <<a<<endl;
Output:
The answer after implicit typecasting is: 1.42857