4. Operators
Activity 4: Fizz Buzz
Solution:
- As usual, we'll start by including the headers we need for the application and starting our main loop:
// Activity 4: Fizz Buzz. #include <iostream> int main() {
- Next, we'll define our loop. We want to print 100 numbers, so we need to iterate 100 times, starting at 1:
for (int i = 1; i <= 100; ++i) {
- The
Fizz Buzz
application tells us that for multiples of 3, we'll printFizz
, and for multiples of 5, we'll printBuzz
instead. However, a number can be a multiple of 3 and 5 at the same time; for example, 15 is a multiple of both, so we'll next define a Boolean value,multiple
, which will help us to keep track of this, giving it an initial value offalse
:bool multiple = false;
- Next, we can check whether our current loop value,
i
, is a multiple of 3. If so, we'll print the wordFizz
and set our multiple Boolean...