Given: A number is considered fizz if it is divisible by 3. It is considered buzz if it is divisible by 5. It is considered fizzbuzz if it is divisible by both 3 and 5. A fizzbuzz is neither fizz nor buzz. Given two numbers n1 and n2 such that n2>n1, let f be the number of fizz, b be the number of buzz and fb be the number of fizzbuzz between n1 and n2(both n1 and n2 are included).
Calculate and return the value of 3*f+5*b-15*fb?
MyApproach:
I used all conditions given in the question but I am confused how to use this condition "A fizzbuzz is neither fizz nor buzz"
I am getting wrong output may be because of this Can Anyone guide me how to solve the problem.
public int calcFB (int n1, int n2)
{
int f=0;
int b=0;
int fb=0;
for(int i=n1;i<=n2;i++)
{
if((i%3==0)&&(i%5==0))
{
fb++;
}
else if(i%3==0)
{
f++;
}
else if(i%5==0)
{
b++;
}
}
return 3*f+5*b+15*fb;
}
For the Input:
Parameters Actual Output Expected Output
'1' '21' 56 18
Aucun commentaire:
Enregistrer un commentaire