The function can be defined the following way
void fizzbuzz(int number_one, int number_two)
{
if ( !( number_two < number_one ) )
{
do
{
if ( !( ( number_one % 3 == 0 ) || ( number_one % 5 == 0 ) ) )
{
printf( "%d", number_one );
}
if ( number_one % 3 == 0 )
{
printf( "Fizz");
}
if ( number_one % 5 == 0 )
{
printf("Buzz");
}
putchar ( '\n' );
} while ( number_one++ != number_two );
}
}
Pay attention to that you should not use the for loop. For example if number_two
is equal to INT_MAX
you will have an infinite loop or undefined behavior.
Also it would be better to use named constants instead of the magic number 3
and 5
. In this case to change the divisors in the function it will be enough to change only one declaration. For example
void fizzbuzz(int number_one, int number_two)
{
const int first_divisor = 3, second_divisor = 5;
if ( !( number_two < number_one ) )
{
do
{
if ( !( ( number_one % first_divisor == 0) || ( number_one % second_divisor == 0 ) ) )
{
printf( "%d", number_one );
}
if ( number_one % first_divisor == 0 )
{
printf( "Fizz");
}
if ( number_one % second_divisor == 0 )
{
printf("Buzz");
}
putchar ( '\n' );
} while ( number_one++ != number_two );
}
}
Here is a demonstration program.
#include <stdio.h>
#include <limits.h>
void fizzbuzz( int number_one, int number_two )
{
const int first_divisor = 3, second_divisor = 5;
if (!( number_two < number_one ))
{
do
{
if (!( ( number_one % first_divisor == 0 ) || ( number_one % second_divisor == 0 ) ))
{
printf( "%d", number_one );
}
if (number_one % first_divisor == 0 )
{
printf( "Fizz" );
}
if (number_one % second_divisor == 0 )
{
printf( "Buzz" );
}
putchar( '\n' );
} while (number_one++ != number_two);
}
}
int main( void )
{
fizzbuzz( INT_MAX - 20, INT_MAX );
}
The program output is
2147483627
Fizz
2147483629
Buzz
Fizz
2147483632
2147483633
Fizz
Buzz
2147483636
Fizz
2147483638
2147483639
FizzBuzz
2147483641
2147483642
Fizz
2147483644
Buzz
Fizz
2147483647
Try this call
fizzbuzz( INT_MAX - 20, INT_MAX );
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
void fizzbuzz( int number_one, int number_two )
{
const int first_divisor = 3, second_divisor = 5;
if (!( number_two < number_one ))
{
do
{
if (!( ( number_one % first_divisor == 0 ) || ( number_one % second_divisor == 0 ) ))
{
printf( "%d", number_one );
}
if (number_one % first_divisor == 0 )
{
printf( "Fizz" );
}
if (number_one % second_divisor == 0 )
{
printf( number_one % first_divisor == 0 ? "buzz" : "Buzz" );
}
putchar( '\n' );
} while (number_one++ != number_two);
}
}