# Language vs algorithm part 2: FizzBuzz

Let’s make this idea of posts i have more interesting.
From now on this will be a Wiki for who wants to add a language. Please comment below which languages you have added or contributed.

The problem this time is FizzBuzz.

FizzBuzz is 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”.

Because this is sometimes used in interviews to test basic programming skills, I will only add one example and hope you all to contribute in other languages.

Python one-liner

``````for i in range(1,101): print("FizzBuzz" if i%15==0 else "Fizz" if i%3==0 else "Buzz" if i%5==0 else i)
``````

Python (switch)

``````for i in range(1, 101):
match i % 15:
case 0:
print("FizzBuzz")
case 3 | 6 | 9 | 12:
print("Fizz")
case 5 | 10:
print("Buzz")
case _:
print(i)
``````

JS one-liner

``````new Array(101).fill(0).forEach((_, i) => console.log(i % 3 === 0 && i % 5 === 0 ? "FizzBuzz" : (i % 3 === 0 ? "Fizz" : (i % 5 === 0 ? "Buzz" : i))))
``````

JavaScript (switch):

``````for (let i = 1; i <= 100; i++) {
switch (true) {
case i % 15 === 0:
console.log("FizzBuzz");
break;
case i % 3 === 0:
console.log("Fizz");
break;
case i % 5 === 0:
console.log("Buzz");
break;
default:
console.log(i);
break;
}
}
``````

JavaScript (if):

``````for (let i = 1; i <= 100; i++) {
if (i % 3 === 0 && i % 5 === 0) {
console.log("FizzBuzz");
} else if (i % 3 === 0) {
console.log("Fizz");
} else if (i % 5 === 0) {
console.log("Buzz");
} else {
console.log(i);
}
}
``````

Swift

``````for i in 1...100 {
switch i % 15 {
case 0:
print("FizzBuzz")
case 3, 6, 9, 12:
print("Fizz")
case 5, 10:
print("Buzz")
default:
print(i)
}
}
``````

C (if):

``````#include <stdio.h>
/* There's no reason to care about parameters */
int main() {
for(int q=0;q<101;q++) {
/* Most common edge case first */
if(q%3==0) printf("Fizz\n");
else if(q%5==0) printf("Buzz\n");
else if(q%15==0) printf("FizzBuzz\n");
else printf("%d\n", q);
}
}
``````

Lisp:

``````(defun is-multiple (i against)
(= (mod i against) 0))
(defun fizzbuzz (i)
(let ((p (concatenate 'string (if (is-multiple i 3) "Fizz" "") (if (is-multiple i 5) "Buzz" ""))))
(print (if (string= p "") i p))))
(dotimes (i 101) (fizzbuzz i))
``````

Rust:

``````fn main() {
let mut count = 1;
while count < 101{
if count % 3 == 0 && count % 5 == 0{
println!("FizzBuzz");
} else if count % 3 == 0{
println!("Fizz");
} else if count % 5 == 0{
println!("Buzz");
} else{
println!("{count}");
}
count = count + 1;
}
}
``````
3 Likes

@whileTRUEpass
(Added JS switch and if code)

1 Like

It’s a wiki post, you can edit those in.

1 Like

JS one-liner

``````new Array(101).fill(0).forEach((_, i) => console.log(i % 3 === 0 && i % 5 === 0 ? "FizzBuzz" : (i % 3 === 0 ? "Fizz" : (i % 5 === 0 ? "Buzz" : i))))
``````
2 Likes

Please copy it in the WIKI and just make the world know in the comment stating what you added (or modified). Love people start contributing! Thanks.

2 Likes

``````for i in 1...100 {
switch i % 15 {
case 0:
print("FizzBuzz")
case 3, 6, 9, 12:
print("Fizz")
case 5, 10:
print("Buzz")
default:
print(i)
}
}
``````

Temporary reply for C version (I’ll remove this once I add it):
C (if):

``````#include <stdio.h>
/* There's no reason to care about parameters */
int main() {
for(int q=0;q<101;q++) {
/* Most common edge case first */
if(q%3==0) printf("Fizz\n");
else if(q%5==0) printf("Buzz\n");
else if(q%15==0) printf("FizzBuzz\n");
else printf("%d\n", q);
}
}
``````

``````~/yc-community\$ touch whee.c
~/yc-community\$ gcc whee.c -o whee
~/yc-community\$ ./whee
Fizz
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
Fizz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
Fizz
31
32
Fizz
34
Buzz
Fizz
37
38
Fizz
Buzz
41
Fizz
43
44
Fizz
46
47
Fizz
49
Buzz
Fizz
52
53
Fizz
Buzz
56
Fizz
58
59
Fizz
61
62
Fizz
64
Buzz
Fizz
67
68
Fizz
Buzz
71
Fizz
73
74
Fizz
76
77
Fizz
79
Buzz
Fizz
82
83
Fizz
Buzz
86
Fizz
88
89
Fizz
91
92
Fizz
94
Buzz
Fizz
97
98
Fizz
Buzz
~/yc-community\$
``````

I used a repl on a team @youngchief created for testing because it was the easiest thing to use

1 Like

1 Like

I had no idea that you could do anything like that in Python. I’ve never seen `match`, `case`, or `|` in Python before.

Actually, it’s shorter the normal way by one line (ignoring that you could put each `print` on the same line as the corresponding `if`).

``````for i in range(1, 101):
if i % 15 == 0:
print("FizzBuzz")
elif i % 3 == 0:
print("Fizz")
elif i % 5 == 0:
print("Buzz")
else:
print(i)
``````
1 Like

I made a Python one-liner for you without using `exec`:

``````for i in range(1,101): print("FizzBuzz" if i%15==0 else "Fizz" if i%3==0 else "Buzz" if i%5==0 else i)
``````
2 Likes

Time to do BrainF or Assembly

1 Like

That would be pretty interesting to see

1 Like

BrainF is somehow easier lol

1 Like

Thanks for who has contributes so far. I will add a new one this week end.
I hope this helps not only to see languages but soon some more interesting problems will pop up (I hope interesting)

1 Like