Advertisement
tepples

Configurable fizzbuzz

Nov 23rd, 2014
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.60 KB | None | 0 0
  1. /*
  2. configurable fizzbuzz in C
  3. 2014 Damian Yerrick, Creative Commons Zero
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. typedef struct FBDivisor {
  9.   unsigned int num;
  10.   const char *symbol;
  11. } FBDivisor;
  12. const FBDivisor divs[] = {
  13.   {3, "fizz"},
  14.   {5, "buzz"}
  15. };
  16. int main(void) {
  17.   for (unsigned int n = 1; n <= 100; ++n) {
  18.     int need_num = 1;
  19.     for (size_t p = 0; p < sizeof(divs)/sizeof(divs[0]); ++p) {
  20.       if (n % divs[p].num == 0) {
  21.         fputs(divs[p].symbol, stdout);
  22.         need_num = 0;
  23.       }
  24.     }
  25.     if (need_num) printf("%u", n);
  26.     putchar('\n');
  27.   }
  28.   return 0;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement