The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 4 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
FizzBuzz Function in C!
So I've never really seen this implemented as a function, but how would I do so in C? A pretty basic question - I want a FizzBuzz function written, and then have it called in main (so not actually written in main). Basically take the attached C program and turn that into a function. That's it! The user should be able to enter a number, say "15", and then it should print out through calling the FizzBuzz function: "1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz" and then the code will terminate. The program I wrote below sets the number at 15, but the goal is for a user to enter any number as long as it's positive.
So the program should look like this (bolded is user input):
Enter a positive number:Â 15
Calling the FizzBuzz function, the output is:
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz
Or if the number was different, it would look like this:
Enter a positive number:Â 33
Calling the FizzBuzz function, the output is:
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz 31 32 Fizz
Â
Â
#include <stdio.h>
int main(void)
{
int i;
for(i=1; i<=15; ++i)
{
if (i % 3 == 0)
printf("Fizz");
if (i % 5 == 0)
printf("Buzz");
if ((i % 3 != 0) && (i % 5 != 0))
printf("%d", i);
printf("\n");
}
} return 0;