Write a program that prints the numbers in the given range. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”. Print a new line after each string or number.
Input Format First line will be the number of testcases, T. Next line will have T integers, denoted by N.
Output Format For each testcase, print the number from 1 to N. But follow the rules given in the problem statement.
Constraints
1 <= T <= 10
N is an integer.
Input File
Expected Output
You can also look at the solution that passes all test cases for this problem.
View C Solution
View Java Solution
Sample Input (Plaintext Link)
2
3 15
Sample Output (Plaintext Link)
1
2
Fizz
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
/* Program */
#include <iostream>
using namespace std;
int main()
{
int n;
int a[10];
// as per constraints given
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
for(int j=0;j<n;j++)
{
for(int c=1;c<=a[j];c++)
{
if(c%3==0 && c%5==0)
cout<<"FizzBuzz"<<endl;
else if(c%3==0)
cout<<"Fizz"<<endl;
else if(c%5==0)
cout<<"Buzz"<<endl;
else
cout<<c<<endl;
}
}
return 0;
}
Input Format First line will be the number of testcases, T. Next line will have T integers, denoted by N.
Output Format For each testcase, print the number from 1 to N. But follow the rules given in the problem statement.
Constraints
1 <= T <= 10
N is an integer.
Input File
Expected Output
You can also look at the solution that passes all test cases for this problem.
View C Solution
View Java Solution
Sample Input (Plaintext Link)
2
3 15
Sample Output (Plaintext Link)
1
2
Fizz
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
/* Program */
#include <iostream>
using namespace std;
int main()
{
int n;
int a[10];
// as per constraints given
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
for(int j=0;j<n;j++)
{
for(int c=1;c<=a[j];c++)
{
if(c%3==0 && c%5==0)
cout<<"FizzBuzz"<<endl;
else if(c%3==0)
cout<<"Fizz"<<endl;
else if(c%5==0)
cout<<"Buzz"<<endl;
else
cout<<c<<endl;
}
}
return 0;
}