This page looks best with JavaScript enabled

Fizz Buzz Implementation in javascript, c++,java,c#,python and php

 ·  β˜• 4 min read  ·  πŸ‘½ john hashim

Fizz Buzz is a very simple programming task, asked in software developer job interviews.

A typical round of Fizz Buzz can be:
Write a program that prints the numbers from 1 to 100 and for multiples of ‘3’ print “Fizz” instead of the number and for the multiples of ‘5’ print “Buzz”.

1
2
3
4
5
6
7
8

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// CPP program to print Fizz Buzz
#include <stdio.h>

int main(void)
{
    int i;
    for (i=1; i<=100; i++)
    {
        // number divisible by 3 and 5 will
        // always be divisible by 15, print 
        // 'FizzBuzz' in place of the number
        if (i%15 == 0)         
            printf ("FizzBuzz\t");     
        
        // number divisible by 3? print 'Fizz'
        // in place of the number
        else if ((i%3) == 0)     
            printf("Fizz\t");                 
        
        // number divisible by 5, print 'Buzz'   
        // in place of the number
        else if ((i%5) == 0)                       
            printf("Buzz\t");                 
    
        else // print the number             
            printf("%d\t", i);                 

    }

    return 0;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Java program to print Fizz Buzz
import java.util.*;
class FizzBuzz
{
    public static void main(String args[])
    { 
        int n = 100;

        // loop for 100 times
        for (int i=1; i<=n; i++)                                 
        {
            if (i%15==0)                                                 
                System.out.print("FizzBuzz"+" "); 
            // number divisible by 5, print 'Buzz' 
            // in place of the number
            else if (i%5==0)     
                System.out.print("Buzz"+" "); 

            // number divisible by 3, print 'Fizz' 
            // in place of the number
            else if (i%3==0)     
                System.out.print("Fizz"+" "); 

            // number divisible by 15(divisible by
            // both 3 & 5), print 'FizzBuzz' in 
            // place of the number
                
            else // print the numbers
                System.out.print(i+" ");                         
        }
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// C# program to print Fizz Buzz
using System;

class GFG
{
    // Driver Code
    public static void Main()
    { 
        int n = 100;

        // loop for 100 times
        for (int i = 1; i <= n; i++)                             
        {
            // number divisible by 5, 
            // print 'Buzz' in place
            // of the number
            if (i % 15 == 0) 
                Console.Write("FizzBuzz" + " "); 

            // number divisible by 3, 
            // print 'Fizz' in place 
            // of the number
            else if (i % 3 == 0)     
                Console.Write("Fizz" + " "); 

            // number divisible by 
            // 15(divisible by both 
            // 3 & 5), print 'FizzBuzz'
            // in place of the number
            else if (i % 5 == 0)                                             
                Console.Write("Buzz" + " "); 
            
            // print the numbers 
            else
                Console.Write(i + " ");                     
        }
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#Python program to print Fizz Buzz
#loop for 100 times i.e. range
for fizzbuzz in range(100): 

    # number divisible by 3, print 'Fizz' 
    # in place of the number
    if fizzbuzz % 15 == 0: 
        print("FizzBuzz")                                         
        continue

    # number divisible by 5, print 'Buzz'
    # in place of the number
    elif fizzbuzz % 3 == 0:     
        print("Fizz")                                         
        continue

    # number divisible by 15 (divisible 
    # by both 3 & 5), print 'FizzBuzz' in
    # place of the number
    elif fizzbuzz % 5 == 0:         
        print("Buzz")                                     
        continue

    # print numbers
    print(fizzbuzz)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php
// PHP program to print Fizz Buzz

$i;
for ($i = 1; $i <= 100; $i++)
{
    // number divisible by 3 and
    // 5 will always be divisible 
    // by 15, print 'FizzBuzz' in
    // place of the number
    if ($i % 15 == 0) 
        echo "FizzBuzz" . "  "; 
    
    // number divisible by 3? print 
    // 'Fizz' in place of the number
    else if (($i % 3) == 0) 
        echo "Fizz" . "  ";             
    
    // number divisible by 5, print 
    // 'Buzz' in place of the number
    else if (($i % 5) == 0)                 
        echo "Buzz" . "  ";             

    else // print the number         
        echo $i,"  " ;             
}

?>
Share on

john hashim
WRITTEN BY
john hashim
Web Developer