55502f40dc8b7c769880b10874abc9d0

My knowledge of C is limited, however I know there are better implementations of this well-known program.

#include <stdio.h>

main()
{
    int i;

    for(i = 1; i <= 1000000; i += 1)
        if(i % 15 == 0)
            printf("BizzBuzz\n");
        else if(i % 3 == 0)
            printf("Bizz\n");
        else if(i % 5 == 0)
            printf("Buzz\n");
        else
            printf("%d\n", i);
}

Refactorings

No refactoring yet !

D41d8cd98f00b204e9800998ecf8427e

fundamental

June 24, 2010, June 24, 2010 23:51, permalink

1 rating. Login to rate!
#include <stdio.h>

int main()
{
    int i;
    for(i=1; i<1000; ++i)
        if(printf("%s%s", i%3?"":"Bizz", i%5?"":"Buzz") > 0)
            putchar('\n');

    return 0;
}
D41d8cd98f00b204e9800998ecf8427e

fundamental

June 24, 2010, June 24, 2010 23:54, permalink

1 rating. Login to rate!

Fixing the missing non-bizz/buzz case

#include <stdio.h>

int main()
{
    int i;
    for(i=1; i<6; ++i) {
        if(printf("%s%s", i%3?"":"Bizz", i%5?"":"Buzz") > 0)
            putchar('\n');
        else
            printf("%d\n",i);
    }

    return 0;
}
55502f40dc8b7c769880b10874abc9d0

thaostra.myopenid.com

June 26, 2010, June 26, 2010 03:33, permalink

1 rating. Login to rate!

This was the result of experimenting with the re-factored code.

#include <stdio.h>

int main()
{
    int i;

    for(i = 1; i <= 100; ++i)
        if(printf("%s", i % 15 == 0 ? "BizzBuzz\n" : i % 3 == 0 ? "Bizz\n" : i % 5 == 0 ? "Buzz\n" : "" ) == 0)
            printf("%d\n",i);

    return 0;
}
A8d3f35baafdaea851914b17dae9e1fc

Adam

June 26, 2010, June 26, 2010 06:45, permalink

1 rating. Login to rate!
#include <stdio.h>

int main(int argc, char **argv)
{
	char *c[]={"BizzBuzz\n","Bizz\n","Buzz\n","%d\n"};	
	for (int i=1;i<=100;i++)
		printf(c[((i%3>0)<<1)+(i%5>0)],i);
}

Your refactoring





Format Copy from initial code

or Cancel