C++ Operators Explained: Arithmetic, Relational, and Logical Operators - Biz Tech

C++ Operators Explained: Arithmetic, Relational, and Logical Operators

Listen
  #include <iostream>

int main() {
    int a = 10;
    int b = 3;

    // Arithmetic Operators
    int sum = a + b; // 13
    int difference = a - b; // 7
    int product = a * b; // 30
    int quotient = a / b; // 3
    int remainder = a % b; // 1

    std::cout << "Arithmetic Operators:" << std::endl;
    std::cout << "Sum: " << sum << ", Difference: " << difference
              << ", Product: " << product << ", Quotient: " << quotient
              << ", Remainder: " << remainder << std::endl;

    // Relational Operators
    bool isEqual = a == b; // false
    bool isNotEqual = a != b; // true
    bool isGreater = a > b; // true
    bool isLess = a < b; // false
    bool isGreaterOrEqual = a >= b; // true
    bool isLessOrEqual = a <= b; // false

    std::cout << "\nRelational Operators:" << std::endl;
    std::cout << "Is Equal: " << isEqual << ", Is Not Equal: " << isNotEqual
              << ", Is Greater: " << isGreater << ", Is Less: " << isLess
              << ", Is Greater Or Equal: " << isGreaterOrEqual
              << ", Is Less Or Equal: " << isLessOrEqual << std::endl;

    // Logical Operators
    bool logicalAnd = (a > 0) && (b > 0); // true
    bool logicalOr = (a < 0) || (b < 0); // false
    bool logicalNot = !(a > 0); // false

    std::cout << "\nLogical Operators:" << std::endl;
    std::cout << "Logical AND: " << logicalAnd << ", Logical OR: " << logicalOr
              << ", Logical NOT: " << logicalNot << std::endl;

    return 0;
}

This code block demonstrates an example of C++ operators, including arithmetic, relational, and logical operators. The program calculates and prints the results of various operations using these operators on two integer variables, a and b.