SourceBae Blogs

C++ Floor Division Operator

In C++, the floor division operator, also known as the integer division operator, is used to divide two integers and return the quotient as an integer. The symbol for the floor division operator is ” and it works similar to the ‘/’ operator but rounds the quotient down to the nearest integer.

For example, consider the following code:

include using namespace std; int main() { cout << 10 / 3 << endl; // 3 cout << 10 / 3.0 << endl; // 3.333333 cout << 10 \ 3 << endl; // 3 return 0; }

As you can see, the first line of code uses the ‘/’ operator to perform true division and returns the quotient as integer (3). The second line of code uses the ‘/’ operator to divide an integer by a float and returns the quotient as a floating-point number (3.333333).The third line of code uses the ” operator to perform floor division and returns the quotient as an integer (3).
It’s also possible to use the floor() function to perform floor division.

include cout << floor(10.0/3.0); // 3

It’s worth noting that in C++, the floor division operator only works with integers, and if one of the operands is a float or a double, it will be truncated to an integer before the division is performed.

include using namespace std; int main() { cout << 10.5 / 3 << endl; // 3 cout << 10 / 3.5 << endl; // 2 return 0; }

In the first line of code, the decimal part of the first operand (10.5) is truncated to 10, and the division is performed as 10/3 = 3. In the second line of code, the decimal part of the second operand (3.5) is truncated to 3, and the division is performed as 10/3 = 2.
It’s also important to note that if the division results in a non-integral quotient, the floor division operator rounds down the quotient to the nearest integer. For example:

include using namespace std; int main() { cout << 7/3 << endl; // 2 return 0; }

Here, the division 7/3 is not a whole number, but the floor division operator rounds it down to 2.

In summary, the floor division operator in C++ is used to divide two integers and return the quotient as an integer. The symbol for the floor division operator is”. The operator works similar to the ‘/’ operator but rounds the quotient down to the nearest integer. It’s important to note that if one of the operands is a float or double, it will be truncated to an integer before the division is performed.

Additionally, if the division results in a non-integral quotient, the floor division operator rounds the quotient down to the nearest integer. It’s also possible to use the floor() function from the cmath library to perform floor division. It’s always recommended to use the floor division operator or the floor() function to perform integer division in C++ to ensure that your code works correctly and the results are as expected.

Share your love
Sourceblogs
Sourceblogs

India’s Leading Talent Marketplace For Remote Developers.

Leave a Reply

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