0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

14言語でFizzBuzz

Last updated at Posted at 2020-07-23

なんとなく触れたことがある言語でFizzBuzzを書いてみようと思ってまとめました。
全くスマートな書き方ではないと思うので注意してください。一応全部実行して確認してはいますが間違っている可能性もあります。

Haskellだけは再帰を使いましたが他は全部for文で書いています。

Python3

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

Ruby

for i in 1..20
  if i%15 == 0
    puts "FizzBuzz"
  elsif i%3 == 0
    puts "Fizz"
  elsif i%5 == 0
    puts "Buzz"
  else
    puts i
  end
end

C

# include <stdio.h>

int main(){
  for(int i=1; i<21; i++){
    if(i%15 == 0)
      printf("FizzBuzz\n");
    else if(i%3 == 0)
      printf("Fizz\n");
    else if(i%5 == 0)
      printf("Buzz\n");
    else
      printf("%d\n", i);
  }
}

C++

# include <iostream>
using namespace std;

int main(){
  for(int i=1; i<21; i++){
    if(i%15 == 0)
      cout << "FizzBuzz" << endl;
    else if(i%3 == 0)
      cout << "Fizz" << endl;
    else if(i%5 == 0)
      cout << "Buzz" << endl;
    else
      cout << i << endl;
  }
}

JavaScript

for(let i=1; i<21; 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);
}

Java

class Main{
  public static void main(String[] args){
    for(int i=1; i<21; i++){
      if(i%15 == 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);
    }
  }
}

Go

package main
import "fmt"

func main(){
  for i:=1; i<21; i++{
    if i%15 == 0{
      fmt.Println("FizzBuzz")
    }else if i%3 == 0{
      fmt.Println("Fizz")
    }else if i%5 == 0{
      fmt.Println("Buzz")
    }else{
      fmt.Println(i)
    }
  }
}

Rust

fn main(){
  for i in 1..20{
    if i%15 == 0{
      println!("FizzBuzz");
    }else if i%3 == 0{
      println!("Fizz");
    }else if i%5 == 0{
      println!("Buzz");
    }else{
      println!("{}", i);
    }
  }
}

Haskell

fb :: Int -> String
fb n
  | mod n 15 == 0 = "FizzBuzz"
  | mod n 3 == 0 = "Fizz"
  | mod n 5 == 0 = "Buzz"
  | otherwise = (show n)

rep :: Int -> IO ()
rep 1 = putStrLn (fb 1)
rep n = do
  rep (n-1)
  putStrLn (fb n)

main :: IO ()
main = rep 20

Nim

for i in 1..20:
  if i mod 15 == 0:
    echo "FizzBuzz"
  elif i mod 3 == 0:
    echo "Fizz"
  elif i mod 5 == 0: 
    echo "Buzz"
  else:
    echo i

PHP

<?php
  for($i=1; $i<21; $i++){
    if($i%15 == 0)
      echo "FizzBuzz\n";
    else if($i%3 == 0)
      echo "Fizz\n";
    else if($i%5 == 0)
      echo "Buzz\n";
    else
      echo $i . "\n";
  }
?>

R

for(i in 1:20){
  if(i%%15 == 0)
    print("FizzBuzz")
  else if(i%%3 == 0)
    print("Fizz")
  else if(i%%5 == 0)
    print("Buzz")
  else
    print(i)
}

VB

Public Class Main
  Shared Sub Main
    For i As Integer = 1 To 20
      If i mod 15 = 0 Then
        Console.WriteLine("FizzBuzz")
      ElseIf i mod 3 = 0 Then
        Console.WriteLine("Fizz")
      Else If i mod 5 = 0 Then
        Console.WriteLine("Buzz")
      Else
        Console.WriteLine(i)
      End If
    Next
  End Sub
End Class

Kotlin

fun main(){
  for(i in 1..20){
    if(i%15 == 0)
      println("FizzBuzz")
    else if(i%3 == 0)
      println("Fizz")
    else if(i%5 == 0)
      println("Buzz")
    else
      println(i)
  }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?