I tried to solve a problem yesterday in c++. The problem was a famous one called "Fizzbuzz". It will take a number as input. For each multiple of 3, print "Fizz". For each multiple of 5, print "Buzz". For numbers which are multiples of both 3 and 5, print "FizzBuzz" instead of the number. If a number which is not multiples of 3, 5 or both it will print the number instead.
An example: If I give 5 as input it will show output like this: 12Fizz4Buzz
. But the problem is this judge has a template code like the following:
#include<bits/stdc++.h>
using namespace std;
int FizzbuzzFunc(int num)
{
return 0;
}
int main()
{
int number;
cin>>number;
cout<<FizzbuzzFunc(number);
return 0;
}
Whenever I tried to solve it by putting my code into that "FizzbuzzFunc" It is printing an extra 0 at the end of the output. An example if my code seems like this:
#include<bits/stdc++.h>
using namespace std;
int FizzbuzzFunc(int num)
{
for(int i = 1; i <= num; i++)
{
if (i % 3 == 0 && i % 5 == 0)
{
cout<<"FizzBuzz";
}
else if(i % 3 == 0)
{
cout<<"Fizz";
}
else if(i % 5 == 0)
{
cout<<"Buzz";
}
else
{
cout<<i;
}
}
return 0;
}
int main()
{
int number;
cin>>number;
cout<<FizzbuzzFunc(number);
return 0;
}
and take input as 5 then the output will look like this: 12Fizz4Buzz0
.
How can I remove that extra 0 from the output?