All lessonsOpen Open Open Open Open
2. Variables and types
Arithmetic
0 of 5 activities0%
Reading 1
Operators and integer division
C++ uses + - * / and % (remainder).
If both operands are integers, / is integer division: it drops the fraction. 7 / 2 is 3, not 3.5.
If at least one operand is a double, / keeps a fractional part: 7.0 / 2 is 3.5.
% only works with integers: 7 % 2 is 1.
int a = 7, b = 2;
cout << a / b << endl; // 3
cout << a % b << endl; // 1Check 2
Integer division
What does this print? int x = 7 / 2; cout << x;
Fill in 3
Remainder
Try it 4
Try the operators
Run it, then change 7 and 2 to other integers.
main.cpp
Loading editor…
Output will appear here.
Assignment 5
Double a number
Read one integer n and print n * 2, followed by a newline.
main.cpp
Loading editor…