B4Coder
0%
LessonAbout 5 min

Chapter 1 · Lesson 3 of 5

03. Numbers and sums

C++ can do sums. Send them to cout and it shows the answer: no placeholders needed.

Numbers go straight to cout, without quotes. If you send a sum, C++ works it out first and prints the answer. Text and numbers can be mixed in one line, each after its own <<.

C++
#include <iostream>using namespace std;
int main() {    cout << "I have " << 12 << " mangoes" << endl;    cout << "7 + 5 = " << 7 + 5 << endl;    cout << 6 << " times " << 7 << " is " << 6 * 7 << endl;    return 0;}

It shows

I have 12 mangoes
7 + 5 = 12
6 times 7 is 42

Use + and - as usual, * to multiply and / to divide, and % for what's left over after dividing.

Your turn

1Steps

  1. 1A cinema has 12 rows of 15 seats. Print Seats: and the number of seats, worked out by C++ with *.
  2. 2Then print Minutes in a day: and 24 * 60, also worked out by C++.

Write it in main.cpp. If it reads input, put the answers in Program input, under the terminal.

Use: cout

Starting code

The editor starts empty. Copy this into it to begin, or write your own from scratch.

C++
#include <iostream>using namespace std;
int main() {    cout << "Seats: " << endl;    return 0;}

2Expected output

It should show

Seats: 180
Minutes in a day: 1440

3Hints

Stuck? Open one hint at a time: each gives a bit more help.

Hint 1

Send the words, then the sum, to cout, each after its own <<.

Hint 2

The first line is cout << "Seats: " << 12 * 15 << endl;

Hint 3

The second is made the same way, with 24 * 60.

Terminal

Click Run to see what your program shows, then Submit answer to check it and unlock Next.