cpp / beginner
Snippet
Arithmetic Operators
Arithmetic operators perform mathematical calculations. The five basic operators are: + (addition), - (subtraction), * (multiplication), / (division), and % (modulus/remainder). Note that dividing two integers returns an integer result, discarding any decimal portion. The modulus operator returns the remainder after division.
snippet.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>int main() {int a = 10, b = 3;std::cout << "Addition: " << (a + b) << std::endl;std::cout << "Subtraction: " << (a - b) << std::endl;std::cout << "Multiplication: " << (a * b) << std::endl;std::cout << "Division: " << (a / b) << std::endl;std::cout << "Modulus: " << (a % b) << std::endl;return 0;}
Breakdown
1
int a = 10, b = 3;
Two integer variables for demonstration
2
a + b
Addition operator, result is 13
3
a - b
Subtraction operator, result is 7
4
a * b
Multiplication operator, result is 30
5
a / b
Integer division, result is 3 (decimal truncated)
6
a % b
Modulus operator, result is 1 (remainder of 10/3)