top of page

Daily Coding Problem #7

Writer's picture: Mischievous_FaceMischievous_Face

Updated: Feb 25, 2019

/* Write a function which, given a numeric input, will print or return:

* The word "Fizz" if the given number is a multiple of 3.

* The word "Buzz" if the given number is a multiple of 5.

* The word "FizzBuzz" if the given number is a multiple of both 3 and 5.

* The string form of the given number, otherwise.


Then write a loop which will call this function for the values 1 through 100,

printing these strings (each on a new line). */


#include <string>

#include <iostream>

using namespace std;


string fizzBuzz(int val)

{

if(val % 3 == 0)

{

if(val % 5 == 0)

{

return "FizzBuzz";

}

else

{

return "Fizz";

}

}

else if(val % 5 == 0)

{

return "Buzz";

}

else

{

return to_string(val);

}

}


int main()

{

string result;

for (int i = 1; i <= 100; ++i)

{

result = fizzBuzz(i);

cout << result << endl;

}


}


11 views0 comments

Recent Posts

See All

Comments


© 2020 Josh Painter

bottom of page