Skip to content

Instantly share code, notes, and snippets.

@mick-io
Last active February 18, 2021 22:25
Show Gist options
  • Save mick-io/6514a2cd4176327ee45f3d0d258b4b79 to your computer and use it in GitHub Desktop.
Save mick-io/6514a2cd4176327ee45f3d0d258b4b79 to your computer and use it in GitHub Desktop.
FizzBuzz solved using a variety of programming languages
#include <stdio.h>
#include <string.h>
void fizzbuzz()
{
for (int i = 1; i < 101; i++)
{
char s[9] = "";
if (i % 3 == 0)
{
strcat(s, "Fizz");
}
if (i % 5 == 0)
{
strcat(s, "Buzz");
}
s[0] == '\0' ? printf("%d\n", i) : printf("%s\n", s);
}
}
int main()
{
fizzbuzz();
return 0;
}
// I don't know C, tell me a better way.
package main
import "fmt"
func main() {
fizzbuzz()
}
func fizzbuzz() {
for n := 1; n < 101; n++ {
s := ""
if n%3 == 0 {
s += "Fizz"
}
if n%5 == 0 {
s += "Buzz"
}
if s != "" {
fmt.Println(s)
} else {
fmt.Println(n)
}
}
}
class Sandbox {
public static void main(String[] args) {
fizzbuzz();
}
private static void fizzbuzz() {
for (int n = 1; n < 101; n++) {
String s = "";
if (n % 3 == 0) {
s += "Fizz";
}
if (n % 5 == 0) {
s += "Buzz";
}
System.out.println(s == "" ? n : s);
}
}
}
#!/usr/bin/env node
function fizzbuzz() {
for (let n = 0; n < 101; n++) {
let s = "";
if (n % 3 === 0) {
s += "Fizz";
}
if (n % 5 === 0) {
s += "Buzz";
}
console.log(s || n);
}
}
fizzbuzz();
#!/usr/bin/env python3
def fizzbuzz():
for n in range(1, 101):
out = ""
if n % 3 == 0:
out += "Fizz"
if n % 5 == 0:
out += "Buzz"
print(out or n)
fizzbuzz()
#!/usr/bin/env ruby
def fizzbuzz
1.upto 100 do |n|
s = ""
s += "Fizz" if n % 3 == 0
s += "Buzz" if n % 5 == 0
puts s != "" ? s : n
end
end
fizzbuzz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment