Answer to Question #271459 in C++ for Nokachi

Question #271459

Remember the game of FizzBuzz from the last time? Well, I thought of some changes in the game, and also with the help of loops as well. Firstly, you'll be asking for a random integer and then loop from 1 until that integer. Then, implement these conditions in the game:

  • print "Fizz" if the number is divisible by 3
  • print "Buzz" if the number is divisible by 5
  • print "FizzBuzz" if the number is divisible by both 3 and 5
  • print the number itself if none of the above conditions are met
1
Expert's answer
2021-11-26T07:08:42-0500
#include <iostream>
using namespace std;
void fizzBuzz(int n){
    if(!(n%3)){
        cout<<"Fizz";
    }
    if(!(n%5)){
        cout<<"Buzz";
    }
    else if(n%3 && n%5){
        cout<<n;
    }
    cout<<endl;
}
int main(){
    int n;
    cout<<"Input the integer: ";
    cin>>n;
    for(int i = 1; i <= n; i++){
        fizzBuzz(i);
    }
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS