All lessons

2. Variables and types

Arithmetic

0 of 5 activities0%

Reading 1

Operators and integer division

Open

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;  // 1

Check 2

Integer division

Open

What does this print? int x = 7 / 2; cout << x;

Fill in 3

Remainder

Open

The operator that gives the remainder after integer division is

Try it 4

Try the operators

Open

Run it, then change 7 and 2 to other integers.

main.cpp
Loading editor…
Output will appear here.

Assignment 5

Double a number

Open

Read one integer n and print n * 2, followed by a newline.

main.cpp
Loading editor…