-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfizzBuzz.cpp
53 lines (44 loc) · 1.16 KB
/
fizzBuzz.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Create a function that takes a number as an argument and returns "Fizz", "Buzz" or "FizzBuzz".
// If the number is a multiple of 3 the output should be "Fizz".
// If the number given is a multiple of 5, the output should be "Buzz".
// If the number given is a multiple of both 3 and 5, the output should be "FizzBuzz".
// If the number is not a multiple of either 3 or 5, the number should be output on its own as shown in the
// examples below.
// The output should always be a string even if it is not a multiple of 3 or 5.
// Examples
// fizzBuzz(3) ➞ "Fizz"
// fizzBuzz(5) ➞ "Buzz"
// fizzBuzz(15) ➞ "FizzBuzz"
// fizzBuzz(4) ➞ "4"
#include <iostream>
#include <string>
using namespace std;
string fizzBuzz(int num)
{
string str;
if (num % 5 == 0 && num % 3 == 0)
{
str = "FizzBuzz";
}
else if (num % 5 == 0)
{
str = "Buzz";
}
else if (num % 3 == 0)
{
str = "Fizz";
}
else
{
str = to_string(num);
}
return str;
}
int main()
{
cout << fizzBuzz(3) << endl;
cout << fizzBuzz(5) << endl;
cout << fizzBuzz(15) << endl;
cout << fizzBuzz(4) << endl;
return 0;
}