FizzBuzz
Fizz buzz is a group word game for children to teach them about division. Players take turns to count incrementally, replacing any number divisible by three with the word "fizz", and any number divisible by five with the word "buzz", and any number divisible by both 3 and 5 with the word "fizzbuzz".
Write a program that counts from 1-200 with FizzBuzz.
Using a for loop; output "Fizz" if the number is divisible by 3, "Buzz" if
it's divisible by 5, and "FizzBuzz" if it's divisible by both.
Otherwise, print the number itself.
Expected Output
1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz, 16, 17, Fizz, 19, Buzz, Fizz, 22, 23, Fizz, Buzz, 26, Fizz, 28, 29, FizzBuzz, 31, 32, Fizz, 34, Buzz, Fizz, ...
Answer
#include <iostream>
int main()
{
for (int i = 1; i <= 200; ++i)
{
if (i % 3 == 0 && i % 5 == 0) {
std::cout << "FizzBuzz\n";
}
else if (i % 3 == 0) {
std::cout << "Fizz\n";
}
else if (i % 5 == 0) {
std::cout << "Buzz\n";
}
else {
std::cout << i << "\n";
}
}
}