Arithmetics

3.1 - Integers type


To store integer numbers (whole numbers), we use the int type.

This line creates an int called age and set the value to 18:

int age = 18;

Arithmetic operations such as addition and subtraction can be applied to integers. This line prints out the age added by 5:

std::cout << "5 years later I will be " << (age + 5) << std::endl;

Note that (age + 5) is enclosed within parentheses to make the fact the + operator is executed before the << operator explicit.

Task

Make the program prints about 20 years ago instead.

  • Change the variable year_span to be 20.
#include <iostream> int main() { int current_year = 2020; int year_span = 10; std::cout << "The current year is " << current_year << std::endl; std::cout << year_span << " years ago was " << (current_year - year_span) << std::endl; }