#include <iostream>
using namespace std;
int main() {
int a = 10, b = 3;
cout << "a + b = " << a + b << endl; // 13
cout << "a - b = " << a - b << endl; // 7
cout << "a * b = " << a * b << endl; // 30
cout << "a / b = " << a / b << endl; // 3 (integer division)
cout << "a % b = " << a % b << endl; // 1
return 0;
}
Assignment Operators
Operator
Equivalent To
x = 5
Assign 5 to x
x += 3
x = x + 3
x -= 3
x = x - 3
x *= 3
x = x * 3
x /= 3
x = x / 3
x %= 3
x = x % 3
Comparison Operators
These operators return true (1) or false (0):
Operator
Meaning
Example
==
Equal to
5 == 5 → true
!=
Not equal
5 != 3 → true
>
Greater than
5 > 3 → true
<
Less than
3 < 5 → true
>=
Greater or equal
5 >= 5 → true
<=
Less or equal
3 <= 5 → true
Logical Operators
Operator
Name
Example
&&
AND
true && false → false
||
OR
true || false → true
!
NOT
!true → false
Exercise
Write a program that reads two numbers and prints: their sum, whether they are equal, and whether the first is greater than the second.
Show Solution
#include <iostream>
using namespace std;
int main() {
int x, y;
cout << "Enter two numbers: "; cin >> x >> y;
cout << "Sum: " << x + y << endl;
cout << "Equal: " << (x == y) << endl;
cout << "x > y: " << (x > y) << endl;
return 0;
}
Quiz
What does the % operator do?
A) Percentage calculation
B) Returns the remainder of division
C) Converts to float
D) None of the above
Answer
B)% is the modulus operator — it returns the remainder after integer division.
What is the result of 10 / 3 when both are integers in C++?
A) 3.33
B) 4
C) 3
D) 1
Answer
C) 3 — integer division truncates the decimal part.