Learn C++ FizzBuzz Solution

My solution for the Learn C++ FizzBuzz Question :slight_smile:

#include <iostream>

int main(){
  
  
  for(int i = 1; i <=100; 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";
    }
  }
  
  return 0 ;
  
}
1 Like

great work! :slight_smile:

there is an official Learn C++ GitHub repo: https://github.com/Codecademy/learn-cpp

(if you want to upload to it)

1 Like

This was my solution, a bit longer but it does the job.

#include <iostream>
#include <string>

std::string finalAnswer = "";

int main() {

  // Brain explodes here:
  for (int i = 1; i <= 100; i++){
    if (i % 3 == 0 && i % 5 == 0){
      finalAnswer = "FizzBuzz";
      std::cout << finalAnswer << std::endl;
    }
    else if (i % 3 == 0){
      finalAnswer = "Fizz";
      std::cout << finalAnswer << std::endl;
    }
    else if (i % 5 == 0){
      finalAnswer = "Buzz";
      std::cout << finalAnswer << std::endl;
    }
    else {
      std::cout << i << std::endl;      
    }
  }
}