Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

FizzBuzz

Name: VIPPER 2011-05-15 3:49

Write a program that prints the integers 1 - 100, but for every multiple of 3, print "Fizz" instead of the number, for every multiple of 5 print "Buzz", and for every multiple of both 3 and 5, print "FizzBuzz".


#include <stdio.h>
#include <stdlib.h>

int main(void)
{
        for (int i = 1; i <= 100; i++) {
                if (i % 3 == 0 && i % 5 == 0)
                        puts("FizzBuzz");
                else if (i % 3 == 0)
                        puts("Fizz");
                else if (i % 5 == 0)
                        puts("Buzz");
                else
                        printf("%d\n", i);
        }
        return EXIT_SUCCESS;
}

Name: Anonymous 2011-05-15 5:02

>>3
Because it is such a simple problem, it easily lends itself to various clever and concise solutions.  For example, solving it with very few lines of code or using more esoteric language features.

for (int i=1; i<101; i++)
                printf("%d %s %s\n", i, i%3 ? "" : "fizz", i%5 ? "" : "buzz");


It's also a classic interview question:
http://www.codinghorror.com/blog/2007/02/why-cant-programmers-program.html

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List