FizzBuzz Solution Dumping Ground

Yeah but a loop from 1 to 10 is like easy peasy. Else you should NOT be programming.

for(int i = 1; i < 10; i++){
out.println(i);
}

Why you have not corrected him from:

a=a|b, b=a^b, a=a^b

to:

a=a^b, b=a^b, a=a^b

before you hired the guy?

Excessively verbose.

In Clojure you’d use (range 1 11)

In Scheme you’d use (iota 10 1)

In Smalltalk there’s 1 to: 10

Honest-to-Christ - although I’ve been programming in C and its step-children (e.g. C++, Java, C#) since 1986, these days whenever I see curly braces I just want to…ahem…look elsewhere…

for ($i = 1; $i < 101; $i++) {
    if ($i % 3 == 0 && $i % 5 == 0) {
        echo 'FizzBuzz, ';
        continue;
    }

    if ($i % 5 == 0) {
        echo 'Buzz, ';
        continue;
    }

    if ($i % 3 == 0) {
        echo 'Fizz, ';
        continue;
    }

    echo $i . ', ';
}

One liner here (sorry for bad spacing, but I tested and it compiled/worked on the first time):

for(int i=1;i<=100;i++)System.out.println((i%3==0)?(i%5==0)?"FizzBuzz":"Fizz":(i%5==0)?"Buzz":i);

If you want a string for some reason, you can probably do Integer(i).toString in place of the plain i.

To do an array, the System.out.println can be replaced with outputArray[i-1]=

Shortest possible* JavaScript 1-liner:

for(var i=0;i++<100;)console.log((i%3?'':'Fizz')+(i%5?'':'Buzz')||i);

*yes… you should take this as a challenge :smirk:

My Python solution:

 for x in range(1,101):
        if x % 3 == 0 and x % 5 == 0:
            x = "FizzBuzz"
        elif x % 3 == 0:
            x = "Fizz"
        elif x % 5 == 0:
            x = "Buzz"
        print x

It was fun.

Java:

static void printFizzBuzz() {
         System.out.println("Printing FizzBuzz");
         
         int max = 100;
         
         for(int i = 0; i <= max; ++i) {
             String string = "";
             if (i % 3 == 0) {
                 string += "Fizz";
             }
             
             if (i % 5 == 0) {
                 string += "Buzz";
             }
             
             if (string.isEmpty()) {
                 string = String.valueOf(i);
             }
             
             System.out.println(string);
         }
     }

A solution in C

#include<stdio.h>
#include<math.h>

void main(){

int i;

for(i=1;i<101;i++){

if(i%3==0 && i%5!=0){
printf("Fizz for i=%d\n\n",i);
}

else if(i%5==0 && i%3!=0){
printf("Buzz for i=%d\n\n",i);
}

else if(i%5==0 && i%3==0){
printf("FizzBuzz for i=%d\n\n",i);
}

else {printf("\ni=%d\n",i);}

}
}

Here’s the code in Java!

public class FizzBuzz 
{
	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);
			}
		}
	}

}

I can’t take credit, but I found a Fizz Buzz solution in Piet:

1 Like

Here it is in Java:

public static main(String[] args) {
	System.out.println("1");
	System.out.println("2");
	System.out.println("Fizz");
	System.out.println("4");
	System.out.println("Buzz");
	System.out.println("Fizz");
	System.out.println("7");
	System.out.println("8");
	System.out.println("Fizz");
	System.out.println("Buzz");
	System.out.println("11");
	System.out.println("Fizz");
	System.out.println("13");
	System.out.println("14");
	System.out.println("FizzBuzz");
	System.out.println("16");
	System.out.println("17");
	System.out.println("Fizz");
	System.out.println("19");
	System.out.println("Buzz");
	System.out.println("Fizz");
	System.out.println("22");
	System.out.println("23");
	System.out.println("Fizz");
	System.out.println("Buzz");
	System.out.println("26");
	System.out.println("Fizz");
	System.out.println("28");
	System.out.println("29");
	System.out.println("FizzBuzz");
	System.out.println("31");
	System.out.println("32");
	System.out.println("Fizz");
	System.out.println("34");
	System.out.println("Buzz");
	System.out.println("Fizz");
	System.out.println("37");
	System.out.println("38");
	System.out.println("Fizz");
	System.out.println("Buzz");
	System.out.println("41");
	System.out.println("Fizz");
	System.out.println("43");
	System.out.println("44");
	System.out.println("FizzBuzz");
	System.out.println("46");
	System.out.println("47");
	System.out.println("Fizz");
	System.out.println("49");
	System.out.println("Buzz");
	System.out.println("Fizz");
	System.out.println("52");
	System.out.println("53");
	System.out.println("Fizz");
	System.out.println("Buzz");
	System.out.println("56");
	System.out.println("Fizz");
	System.out.println("58");
	System.out.println("59");
	System.out.println("FizzBuzz");
	System.out.println("61");
	System.out.println("62");
	System.out.println("Fizz");
	System.out.println("64");
	System.out.println("Buzz");
	System.out.println("Fizz");
	System.out.println("67");
	System.out.println("68");
	System.out.println("Fizz");
	System.out.println("Buzz");
	System.out.println("71");
	System.out.println("Fizz");
	System.out.println("73");
	System.out.println("74");
	System.out.println("FizzBuzz");
	System.out.println("76");
	System.out.println("77");
	System.out.println("Fizz");
	System.out.println("79");
	System.out.println("Buzz");
	System.out.println("Fizz");
	System.out.println("82");
	System.out.println("83");
	System.out.println("Fizz");
	System.out.println("Buzz");
	System.out.println("86");
	System.out.println("Fizz");
	System.out.println("88");
	System.out.println("89");
	System.out.println("FizzBuzz");
	System.out.println("91");
	System.out.println("92");
	System.out.println("Fizz");
	System.out.println("94");
	System.out.println("Buzz");
	System.out.println("Fizz");
	System.out.println("97");
	System.out.println("98");
	System.out.println("Fizz");
	System.out.println("100");
}

PowerShell
for($i = 1;$i -le 100;$i++){

$fizz = $i % 3
$buzz = $i % 5
if($fizz -eq 0 -and $buzz -eq 0){
write-host fizzbuzz
}
if($fizz -eq 0 -and $buzz -ne 0){
write-host fizz
}
if($buzz -eq 0 -and $fizz -ne 0){
write-host buzz
}

}

My weapon of choice for this problem is perl. There is such a thing as a well written perl program, but I deliberately choose to write bad code which still solves the problem.

sub _ { print /::(.*)/ }; *AUTOLOAD = *_; for(1..100) { $f=0, fizz() if 3 == ++$f; $b=0, buzz() if 5 == ++$b; $f && $b && &$_; &{" "} }

Responding to a nine year old thread, because even the answers that get it right seem clunky. Couple of thoughts:

  1. Why post if you haven’t tested? Some of these answers don’t address the specs correctly, or just don’t work.

  2. Don’t be literal - elegance lies in the code you don’t write. Nearly every single answer here makes an unnecessary double-test of each condition.

  3. Stop compensating - condensing code to 1 line with 11 characters isn’t a measure of your coding prowess, it’s an indicator of the maintainability of your code. The compiler is going to reduce it all to the same IL/bytecode/machine code, so if you want hired, make the code you write at an interview readable.

In C#:

//  Write a program that prints the numbers from 1 to 100. But for multiples of three 
//  print “Fizz” instead of the number and for the multiples of five print “Buzz”. 
//  For numbers which are multiples of both three and five print “FizzBuzz”.           
    for (int i = 0; i <= 100; i++) {

        string output = (i%3 == 0) ? "Fizz" : string.Empty;

        if (i%5 == 0) {
            output += "Buzz";
        }
        
        Console.WriteLine(string.IsNullOrEmpty(output) ? i.ToString() : output);

    }
    
}

Feedback welcome.

Just a small python solution, this one is generic, so you can add other factor/word tuples, if you happen to need something special to happen at multiples of 19? No problem, just add one line.

from collections import OrderedDict
rules = OrderedDict([
    (3, 'fizz'),
    (5, 'buzz'),
    (19, 'beer'),
])
for i in range(1, 101):
    output = ''
    for divisor, out in rules.items():
        if i%divisor == 0:
            output += out
    if output == '':
        output = i
    print('{}: {}'.format(i, output))

Or maybe you could even generalize your rules as lambda and evaluate them later in the loop. It would allow more complex “rules” than just a modulo operation. Of course you lose readability.

from collections import OrderedDict
rules = OrderedDict([
    ('fizz', lambda x: x%3 == 0),
    ('buzz', lambda x: x%5 == 0),
])
for i in range(1,101):
    output = ''
    for out, condition in rules.items():
        if condition(i):
            output += out
    if output == '':
        output = i
    print('{}: {}'.format(i, output))

Here is my solution in PHP:

for($x = 1; $x < 100; $x++) {
if(($x % 3) && ($x % 5)) echo $x;
if(!($x % 3)) echo “Fizz”;
if(!($x % 5)) echo “Buzz”;
echo “
”;
}

in JS, king of the web
for(var i = 0; i<100; i++){
if(i % 15 === 0)
console.log(“fizzBuzz”);
else if(i % 3 === 0)
console.log(“fizz”);
else if(i % 5 === 0)
console.log(“buzz”);
else
console.log(i);
}

In Crystal, which is kind of like a compiled ruby-esque language:

Range.new(1, 100).each do |n|
  msg = ""
  if n.modulo(3) == 0
    msg = msg + "Fizz"
  end
  if n.modulo(5) == 0
    msg = msg + "Buzz"
  end
  if msg == ""
    puts n
  else 
    puts msg
  end
end

Not perfect but I just started learning the language, and the language itself isn’t production-ready.

1 Like

Haven’t seen a GAS solution yet. Well, it’s mostly just JS, but I can cheat a bit.

My first attempt just used Logger.log() and was kind of direct brute force, but worked. Though technically, I failed since, in retrospect, the Logger method doesn’t really satisfy ‘print’ from an end user perspective and the Logger ended up string converting i with 1 decimal place. Took about 2 min.

function fizzBuzz() {
  for (var i=1;i<=100;i++){
    if (i%3 == 0 && i%5 == 0){
      Logger.log('FizzBuzz');
    } else if (i%3 == 0) {
      Logger.log('Fizz');
    } else if (i%5 == 0) {
      Logger.log('Buzz');
    } else {
      Logger.log(i);
    }
  }
}

Realizing I technically failed, took an extra 5 to optimise a touch while still having it legible and output as a web app. With GAS, you can kind of cheat and provide partial HTML. It will fill in any headers and such that you’re missing, so the client side works.

Here’s the final:

function doGet() {
  var str, res = '';
  for (var i=1;i<=100;i++){
    if (i % 3 == 0) str += 'Fizz';
    if (i % 5 == 0) str += 'Buzz';
    if (!str) str = i;
    res += str + '<br>';
    str = '';
  }
  return HtmlService.createHtmlOutput(res);
}

I’m not a programmer by trade, but I do dabble in a lot of GAS (which leads to client side JS, jQuery, html, css, etc.), and (thankfully much less frequently) VBA stuff related to my job. Fairly happy with 7 minutes for a working web app that meets the goal while not technically being a programmer. And no, not cheating. Replace exec in the url with edit to see the script.

Frankly shocking to read a lot of these from people who are programmers and missed some of the basic requirements.