Selection and iteration
This lesson contains approximately 25 minutes of video content.
Selection
The if
statement
The switch
statement
Iteration
Iterative statements
Loop control
Activity: Fizz Buzz
Graded Playground Autograder
Activity Prompt:
In this problem, you will write a solution to the Fizz Buzz problem (description to follow) within the body of the function FizzBuzz
. I understand that we haven't discussed functions in detail yet. What you need to know is that our auto-grader will invoke your implementation (the code within the function body) with an integer value to initialize the parameter int n
.
You will write your definition of Fizz Buzz between the curly braces and with respect to the value stored in the parameter n
. Your function will return a string literal based on the value of n:
- The string literal
Fizz
(return "Fizz";
) ifn
is divisible by3
- The string literal
Buzz
ifn
is divisible by5
- The string literal
FizzBuzz
ifn
is divisible by3
and5
- The
n
encoded as a string (return std::to_string(n);
) ifn
is not divisible by3
and/or5
FizzBuzz
in fizz-buzz.cc
. You can test your implementation in driver.cc
by calling FizzBuzz
with different arguments.
#include <string>
std::string FizzBuzz(int n) {
// write your solution here!
}
#include <iostream>
std::string FizzBuzz(int n);
int main() {
std::cout << FizzBuzz(3) << std::endl;
std::cout << FizzBuzz(5) << std::endl;
std::cout << FizzBuzz(7) << std::endl;
std::cout << FizzBuzz(15) << std::endl;
}
Submit is not supported in this site.