Edit: I think I need to be more precise, the code itself is good but I need to modify it not to have a block with more than 3 branches. For instance, one could separate it by doing multiple functions
I am trying to do the Fizzbuzz, my code is right but the problem is that I cannot have more than three branches in a conditional block, here is what I've done so far:
#include <stdio.h>
#include <unistd.h>
void fizzbuzz(int number_one, int number_two)
{
for (int nbr = number_one; nbr <= number_two; nbr++) {
if (nbr % 15 == 0)
printf("Fizzbuzz\n");
else if (nbr % 5 == 0)
printf("Buzz\n");
else if (nbr % 3 == 0)
printf("Fizz\n");
else
printf("%i\n", nbr);
}
}
int main(int ac, char **av)
{
if (ac == 1)
return 84;
if (ac == 3) {
int number_one = atoi(av[1]);
int number_two = atoi(av[2]);
if (number_one > number_two) {
printf("Error: the second parameter must");
printf("be greater than the first one.\n");
return 84;
} else
fizzbuzz(number_one, number_two);
}
return 0;
}
The function can be defined the following way
Pay attention to that you should not use the for loop. For example if
number_twois equal toINT_MAXyou will have an infinite loop or undefined behavior.Also it would be better to use named constants instead of the magic number
3and5. In this case to change the divisors in the function it will be enough to change only one declaration. For exampleHere is a demonstration program.
The program output is
Try this call
with other presented programs in answers where there is used the for loop.:)
If you want to output
"Fizzbuzz"instead of"FizzBuzz"then the function can look for example the following way