Input a random positive integer in one line. This will serve as the ending point of your loop.
Using a for() loop, loop from 1 to the ending point (inclusive) and perform the following statements:
If the number is only divisible by 3, print "Fizz"
If the number is only divisible by 5, print "Buzz"
If the number is divisible by both 3 and 5, print "FizzBuzz"
If nothing is true in the previous conditions, skip the number
#include <iostream>
using namespace std;
int main(){
int number;
cout<<"Enter number: ";
cin>>number;
for(int i=1;i<=number;i++){
//If the number is only divisible by 3, print "Fizz"
if(i%3==0){
cout<<"Fizz\n";
}
//If the number is only divisible by 5, print "Buzz"
if(i%5==0){
cout<<"Buzz\n";
}
//If the number is divisible by both 3 and 5, print "FizzBuzz"
if(i%3==0 && i%5==0){
cout<<"FizzBuzz\n";
}
}
cin>>number;
return 0;
}
Comments
Leave a comment