ComputerScienceExpert

(11)

$18/per page/

About ComputerScienceExpert

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Applied Sciences,Calculus See all
Applied Sciences,Calculus,Chemistry,Computer Science,Environmental science,Information Systems,Science Hide all
Teaching Since: Apr 2017
Last Sign in: 103 Weeks Ago, 4 Days Ago
Questions Answered: 4870
Tutorials Posted: 4863

Education

  • MBA IT, Mater in Science and Technology
    Devry
    Jul-1996 - Jul-2000

Experience

  • Professor
    Devry University
    Mar-2010 - Oct-2016

Category > Programming Posted 02 May 2017 My Price 9.00

FizzBuzz Function in C!

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;

Answers

(11)
Status NEW Posted 02 May 2017 06:05 AM My Price 9.00

-----------

Not Rated(0)