Advertisement
banovski

Formatted "Fizzbuzz"

Aug 14th, 2024 (edited)
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.01 KB | Source Code | 0 0
  1. /* "Fizzbuzz" variation: values are first stored in an array and only
  2.    then printed to stdout, colored and with set width.*/
  3.  
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. int main()
  8. {
  9.     char words[101][9];
  10.     char coloring[101][13];
  11.     int i;
  12.     for(i = 1; i <= 100; i++){
  13.         if(i % 15 == 0) {
  14.             strcpy(words[i], "fizzbuzz");
  15.             strcpy(coloring[i], "\033[31;1m");
  16.         }
  17.         else if(i % 5 == 0) {
  18.             strcpy(words[i], "buzz");
  19.             strcpy(coloring[i], "\033[32;1m");
  20.         }
  21.         else if(i % 3 == 0) {
  22.             strcpy(words[i], "fizz");
  23.             strcpy(coloring[i], "\033[34;1m");
  24.         }
  25.         else {
  26.             (void)snprintf(words[i], 3, "%d", i);
  27.             strcpy(coloring[i], "\033[36;1m");
  28.         }
  29.     }
  30.  
  31.     char sep[] = "   \t";
  32.     for(i = 1; i <= 100; i++) {
  33.         printf("%s%2d\033[0m: %s%s", coloring[i], i, words[i], sep);
  34.         if(i % 10 == 0) {
  35.             (void)putchar('\n');
  36.         }
  37.     }
  38.     return 0;
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement