Something really out of my comfort zone here. I completed the C++ course on codecademy and did what I normally do when I’m learning a new programming language. I try to make the FizzBuzz game in as many different ways as I can to get used to the syntax and different operations.
This is was the first attempt at it and I think the easiest to do. It’s not clean but I like learning and you need to make mistakes in order to really learn something:
#include <iostream> using namespace std; int main() { for (int i = 0; i < 101; i++){ if (i % 5 == 0 & i % 3 == 0){ std::cout << "FizzBuzz\n"; }else if (i % 5 == 0){ std::cout << "Fizz\n"; }else if (i % 3 == 0){ std::cout << "Buzz\n"; }else{ std::cout << i << "\n"; } } }
My second iteration was a little more sophisticated and would make any alterations to the rules of FizzBuzz easier to account for:
#include <iostream> #include <string> using namespace std; int main() { for (int i = 0; i < 101; i++){ string output; output = ""; if ( i % 5 == 0 ){ output += "Fizz"; } if ( i % 3 == 0 ){ output += "Buzz"; } if ( output == "" ){ output = to_string(i); } cout << output << "\n"; } }
No doubt that I will be jumping back to create new ways when I have the time. Let me know what you come up with.
Enjoy!