You are viewing a single comment's thread from:
RE: Introducing the Coding Challenge
5 minute C/C++ solution. Taking more time I'd look at getting rid of the trailing ',' in the loop through the function calls, and maybe do some error checking regarding sending wrong type to the function, but for now I think this is pretty simple and the comments make it self explanatory.
Code:
#include <stdio.h>
using namespace std;
//function fizzbuzz takes in an integer and prints out the intended output for that integer
void fizzbuzz (int n) {
//if the number is divisible by 15
if (n%15 == 0) {
printf("FizzBuzz, ");
}
//if the number is divisible by 5
else if (n%5 == 0) {
printf("Buzz, ");
}
//if the number is divisible by 3
else if (n%3 == 0) {
printf("Fizz, ");
}
//if the number is not divisible by 3 or 5 (or 15)
else {
printf("%i, ", n);
}
} //END fizzbuzz
int main (int argc, char* argv[]) {
//test casses
printf("Test Cases: ");
fizzbuzz(0); //prints "FizzBuzz"
fizzbuzz(15); //prints "FizzBuzz"
fizzbuzz(5); //prints "Buzz"
fizzbuzz(3); //prints "Fizz"
fizzbuzz(22); //prints "22"
fizzbuzz(-5); //prints "Buzz"
fizzbuzz(-98); //prints "-98"
printf("End Test Cases\n\n");
//loop through numbers 0 to 100 and call fizzbuzz on each
for (int i = 0; i < 101; i++) {
fizzbuzz(i);
}
} //END main
Console Output
Test Cases: FizzBuzz, FizzBuzz, Buzz, Fizz, 22, Buzz, -98, End Test Cases
FizzBuzz, 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, 34, Buzz, Fizz, 37, 38, Fizz, Buzz, 41, Fizz, 43, 44, FizzBuzz, 46, 47, Fizz, 49, Buzz, Fizz, 52, 53, Fizz, Buzz, 56, Fizz, 58, 59, FizzBuzz, 61, 62, Fizz, 64, Buzz, Fizz, 67, 68, Fizz, Buzz, 71, Fizz, 73, 74, FizzBuzz, 76, 77, Fizz, 79, Buzz, Fizz, 82, 83, Fizz, Buzz, 86, Fizz, 88, 89, FizzBuzz, 91, 92, Fizz, 94, Buzz, Fizz, 97, 98, Fizz, Buzz,
Looking good to me :)