Assume that two int constants, FIRST_YEAR and LAST_YEAR have already been declared and initialized with year values (like 2009, 2014), along with a double variable oil that has been initialized with the number of barrels of oil consumed in Canada in the year given by FIRST_YEAR. Write some code that uses a while statement to print on a line by itself, each of the years from FIRST_YEAR to LAST_YEAR inclusive. On each line, after the year, separated by a colon and a space, print the new value amount of oil, taking into account that each year the oil consumed increases by 20%.

Assume that two int constants, FIRST_YEAR and LAST_YEAR have already been declared and initialized  with year values  (like 2009, 2014), along with a double variable  oil that has been initialized  with the number of barrels of oil consumed in Canada in the year given  by FIRST_YEAR. Write some code that uses a while statement  to print on a line by itself, each of the years from FIRST_YEAR to LAST_YEAR inclusive. On each line, after the year, separated by a colon and a space, print the new value  amount of oil, taking into account that each year the oil consumed increases by 20%.

LANGUAGE: C++

CHALLENGE:

Assume that two int constants, FIRST_YEAR and LAST_YEAR have already been declared and initialized with year values (like 2009, 2014), along with a double variable oil that has been initialized with the number of barrels of oil consumed in Canada in the year given by FIRST_YEAR. Write some code that uses a while statement to print on a line by itself, each of the years from FIRST_YEAR to LAST_YEAR inclusive. On each line, after the year, separated by a colon and a space, print the new value amount of oil, taking into account that each year the oil consumed increases by 20%.

SOLUTION:


int currentYear = FIRST_YEAR;

while (currentYear <= LAST_YEAR) {
   cout << currentYear++ << ": " << oil << '\n';
   oil *= 1.2;
}

Add comment