Search Results


Friday, April 03, 2020

Fizzbuzz Program

Python 1
for num in range(1, 21):
    if num % 3 == 0 and num % 5 == 0:
        print('FizzBuzz')
    elif num % 3 == 0:
        print('Fizz')
    elif num % 5 == 0:
        print('Buzz')
    else:

        print(num)

Python2
for i in range(1, 21):
    print ("Fizz"*(i%3==0)+"Buzz"*(i%5==0) or str(i) )


Powershell
for ($i = 1; $i -lt 21; $i++) {
  $output = ""

  if ($i % 3 -eq 0) {
    $output += "Fizz"
  }

  if ($i % 5 -eq 0) {
    $output += "Buzz"
  }

  if ($output -eq "") {
    $output = $i
  }  Write-Output $output
}


Bash script
#!/bin/bash

x=1

while [ $x -le 21 ]
do
  if [[ 0 -eq "($x%15)" ]]
  then
    echo "fizz buzz"
  elif [[ 0 -eq "($x%5)" ]]
  then
    echo "buzz"
  elif [[ 0 -eq "($x%3)" ]]
  then
    echo "fizz"
   else
    echo "$x"
   fi   
  x=$(( $x + 1 ))
Done



Javascript
console.log("One Liner");
for (let i = 0i < 21; )
  console.log((++i % 3 ? "" : "fizz") + (i % 5 ? "" : "buzz") || i);
console.log("Simpler");

for (var i = 1i < 21i++) {
  if (i % 15 === 0console.log("FizzBuzz");
  else if (i % 3 === 0console.log("Fizz");
  else if (i % 5 === 0console.log("Buzz");
  else console.log(i);
}



Java
package client;

public class Fizzbuzz {
    public static void main(String[] args) {
        IntStream.range(1, 16)
.mapToObj(i -> {
if (i % 15 == 0)
return "FizzBuzz";
else if (i % 3 == 0)
return "Fizz";
else if (i % 5 == 0)
return "Buzz";
else
return "" + i;
}).forEach(System.out::println);
       }
   }
}

C
#include

int
main ()
{
  int i;

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

  return 0;
}

Go-Lang
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)
        }
    }
}


PL/SQL
set serveroutput on;

declare
    i number := 0;
begin
    for i in 1..21
    loop
        if mod(i, 3) = 0 and mod(i, 5) = 0 then
            dbms_output.put_line('FizzBuzz');
        elsif mod(i, 3) = 0 then
            dbms_output.put_line('Fizz');
        elsif mod(i, 5) = 0 then
            dbms_output.put_line('Buzz');
        else
            dbms_output.put_line(to_char(i));
        end if;
    end loop;
end;
/


Oracle SQL
select rownum i,
       case
       when mod(rownum, 3) = 0 and mod(rownum, 5) = 0 then 'FizzBuzz'
       when mod(rownum, 3) = 0 then 'Fizz'
       when mod(rownum, 5) = 0 then 'Buzz'
       else to_char(rownum)
       end
from dual
connect by level < 21;

No comments :