If the number is divided by 3 and 5, then it is divided by 15, therefore:
for each number 1 to 100: if number % 15 == 0: print number, "fizzbuzz" else if number % 5 == 0: print number, "buzz" else if number % 3 == 0: print number, "fizz" else: print number
Other than that, you probably wonβt get it much shorter, at least in a regular language like C (and I assume you donβt want regular code style and golf style modifications to make your code unreadable).
You could also get all this in two lines if you packed the entire main
function on one big line, but I hope that you won't be after this cheat either.
You can get it faster (although you should check all the performance requirements for yourself) with something like:
static const char *xyzzy[] = { "", "", "fizz", "", "buzz", "fizz", "", "", "fizz", "buzz", "", "fizz", "", "buzz", "fizzbuzz", // Duplicate those last three lines to have seven copies (7x15=105). }; for (int i = 1; i <= 100; i++) printf ("%d %s\n", i, xyzzy[i-1]);
As an aside, this array of char pointers is likely to be cheaper than you think, thanks to constant concatenation - in other words, it is likely that there will be only one of each line of C.
As I said, do I need to test faster. In addition, only the shortest code is called in your original specifications, so it may not be relevant.