1. FizzBuzz
by CodeChum Admin
Let’s play a game of FizzBuzz! It’s quite the same with your childhood “PopCorn” game, but with a little bit of twist to align it with programming.
Are you ready?
Instructions:
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
Input
A line containing an integer.
15
Output
Multiple lines containing a string.
Fizz
Buzz
Fizz
Fizz
Buzz
Fizz
FizzBuzz
#include <iostream>
using namespace std;
int main()
{
int number;
cout<<"Input a random positive integer in one line: ";
cin>>number;
cout<<"Multiple lines containing a string:\n";
for(int i=1;i<=number;i++)
{
if((i%3==0)&&(i%5==0))
{
cout<<"FizzBuZZ\n";
}
else if(i%3==0)
{
cout<<"Fizz\n";
}
else if(i%5==0)
{
cout<<"Buzz\n"; }
}
}
Comments
Leave a comment