Introduction
Operators help us do math and compare things easily in code. They make programs simple and clear.
Operators help us do math and compare things easily in code. They make programs simple and clear.
result = value1 operator value2;
Operators work between values to produce a result.
Common operators include +, -, *, /, %, ==, !=, <, >.
int sum = 5 + 3;
bool isEqual = (a == b);
count++;
This program shows how operators do math and compare values. It prints the results clearly.
#include <iostream> int main() { int a = 10; int b = 3; int sum = a + b; // Add int diff = a - b; // Subtract int product = a * b; // Multiply int quotient = a / b; // Divide int remainder = a % b; // Modulus bool isEqual = (a == b); // Compare equality std::cout << "Sum: " << sum << "\n"; std::cout << "Difference: " << diff << "\n"; std::cout << "Product: " << product << "\n"; std::cout << "Quotient: " << quotient << "\n"; std::cout << "Remainder: " << remainder << "\n"; std::cout << "Are a and b equal? " << (isEqual ? "Yes" : "No") << "\n"; return 0; }
Operators make code shorter and easier to read.
Using the wrong operator can cause bugs, so choose carefully.
Operators let you do math and comparisons in code.
They help make programs simple and clear.
Common operators include +, -, *, /, %, and comparison operators.
+ and - in C++?+ and - are symbols that perform mathematical operations.+ operator adds numbers, and - subtracts numbers, enabling calculations in code.*, not x or ..* correctly between two numbers, making it valid syntax.int a = 10; int b = 3; int c = a % b; std::cout << c;
% operator gives the remainder of division. Here, 10 divided by 3 leaves remainder 1.c is assigned 1, which is printed.int x = 5; int y = 10; int z = x + y - ; std::cout << z;
x + y - ; ends with a minus operator but no number or variable after it.- need two operands. Missing the second operand causes a syntax error.n is even or odd in C++. Which operator helps you do this efficiently?% returns the remainder after division, so n % 2 tells if n is even or odd.% [OK]Pass the test and this pattern moves unknown