• Home
  • 3.4 Division Operator (/)

3.4 Division Operator (/)

View Categories

3.4 Division Operator (/)

1 min read

Table of Contents

The division operator (/) divides the left operand by the right operand and returns the quotient. The type of the result depends on the data types of the operands. If both operands are integers, integer division is performed and any fractional part is discarded. If one or both operands are floating-point values, floating-point division is performed.

When performing division, the right operand must not be zero. Dividing by zero results in undefined behavior for integer types.

Source Code #

#include <iostream>

int main()
{
    int num1 = 25;
    int num2 = 4;

    int quotient = num1 / num2;

    std::cout << "First Number : " << num1 << '\n';
    std::cout << "Second Number: " << num2 << '\n';
    std::cout << "Quotient     : " << quotient << '\n';

    return 0;
}

Output #

First Number : 25
Second Number: 4
Quotient     : 6

Explanation #

  • The division operator (/) divides the value of the left operand by the value of the right operand.
  • num1 / num2 performs integer division because both operands are of type int.
  • In integer division, the fractional part of the result is discarded rather than rounded.
  • The resulting value is assigned to the variable quotient.
  • If either operand is a floating-point type, the result of the division is also a floating-point value.
  • The divisor (right operand) should never be zero when performing division.
  • std::cout displays the quotient on the console.
  • return 0; terminates the program successfully.

Powered by BetterDocs

Leave a Reply

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