Fizzbuzz

twocows

The not-so-black cat of ill omen
  • 4,294
    Posts
    16
    Years
    All you would-be programmers out there, here's a fairly simple program that you should be able to write in under 5 minutes, 10 tops. If you can't do it, you need to work on your problem solving A LOT. It's an extremely easy program that's often used to screen out people who don't know what they're doing when applying for jobs.

    Output the numbers from 1 to 100, except replace multiples of 3 with "fizz," multiples of 5 with "buzz," and multiples of 3 AND 5 with "fizzbuzz."

    I was able to think of a solution in two minutes and write the code in C in another two, and this was at 3:00 AM. Give it a try.
     
    easy...

    #include <iostream>
    using namespace std;
    void main()
    {
    cout << "1\n2\nfizz\n4\nbuzz\nfizz\n7\n8\nfizz\nbuzz\n11\nfizz\n13\n14\nfizzbuzz\n16\n17\nfizz\n19\nbuzz\nfizz\n22\n23\nfizz\nbuzz\n26\nfizz\n28\n29\nfizzbuzz\n31\n32\nfizz\n34\nbuzz\nfizz\n37\n38\nfizz\nbuzz\n41\nfizz\n43\n44\nfizzbuzz\n46\n47\nfizz\n49\nbuzz\nfizz\n52\n53\nfizz\n55\n56\nfizz\n58\n59\nfizzbuzz\n61\n62\nfizz\n64\nbuzz\nfizz\n67\n68\nfizz\nbuzz\n71\nfizz\n73\n74\nfizzbuzz\n76\n77\nfizz\n79\nbuzz\nfizz\n82\n83\nfizz\nbuzz\n86\nfizz\n88\n89\nfizzbuzz\n91\n92\nfizz\n94\nbuzz\n96\n97\n98\nfizz\nbuzz";
    }
     
    Last edited:
    easy...

    #include <iostream>
    using namespace std;
    void main()
    {
    cout <<
    Spoiler:

    }

    LOL at the hard-code! XD

    That isn't really that hard too be honest. We have to do things like that in C++ every week for our hand-ins ><
     
    This seems like it would be incredibly easy to make in Python. Just output 1-100 with a simple if and else to check if it's a multiple of 3 or 5
     
    23 lines in Vala.
    Code:
    public static int main (string[] args) {
        string re;
        
        for (int i = 1; i < 101; i++) {
            re = "";
            
            if (i % 3 == 0) {
                re += "fizz";
            }
            
            if (i % 5 == 0) {
                re += "buzz";
            }
            
            if (re != "") {
                stdout.printf ("%s\n", re);
            } else {
                stdout.printf ("%d\n", i);
            }
        }
        
        return 0;
    }
     
    Don't forget multiples of 3 and 5.

    Here is my code. I didn't include it initally to give people a chance to try it out.

    Code:
    // |fizzbuzz.c|: Fizzbuzz
    
    #include <stdio.h>
    
    int main()
    {
        for(int a = 1; a < 101; ++a)
        {
            if (a % 3 == 0 && a % 5 == 0)
                puts("Fizzbuzz");
            else if (a % 3 == 0)
                puts("Fizz");
            else if (a % 5 == 0)
                puts("Buzz");
            else
                printf("%i\n", a);
        }
    }
    And yeah, most of the stuff I do in my C assignments is much harder than this.
     
    I didn't. I made it this way:

    • Start with an empty string
    • If i is a multiple of 3, add "fizz"
    • If i is a multiple of 5, add "buzz"
    In other words: if i is a multiple of both numbers, the string will also have both "fizz" and "buzz" in it.
    Ah, I see, not bad. That's probably more efficient, actually.
     
    Gotta love being late to the party:
    Code:
    public class Lol {
        public static void main(String args[]) {
            for (int i = 1; i < 101; i++) {
                if (i % 3 == 0 && i % 5 == 0)
                    System.out.println("Fizzbuzz");
                else if (i % 3 == 0)
                    System.out.println("Fizz");
                else if (i % 5 == 0)
                    System.out.println("Buzz");
                else System.out.println(i);
            }
        }
    }
    Anything else? :P
     
    Back
    Top