FizzBuzz Test

pradeep's Avatar, Join Date: Apr 2005
Team Leader
Recently I came across an article where the author finds most of the candidates interviewed for the post of a developer can't write basic code, so he gave everyone a simple test called "FizzBuzz Test", which is as follows:

Write 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".

Read more here http://imranontech.com/2007/01/24/us...o-grok-coding/

Some newbies in the forum might consider posting a solution the problem in wichever language they wish to.
1
Beverly P F's Avatar, Join Date: May 2014
Newbie Member
Hi, this is fun
Some languages are missing, here is C#:
Code:
for (int i = 1; i <= 100; i++)
{
    string result = null;

    if (i % 3 == 0)
        result += "Fizz";

    if (i % 5 == 0)
        result += "Buzz";

    Console.WriteLine("{0}", result ?? i.ToString());
}
And here is VB.NET:
Code:
For i As Integer = 1 To 100
    Dim result As String = Nothing

    If i Mod 3 = 0 Then
        result += "Fizz"
    End If

    If i Mod 5 = 0 Then
        result += "Buzz"
    End If

    Console.WriteLine("{0}", If(result, i.ToString()))
Next
Also there is a FizzBuzz enterprise edition on GitHub, I laughed my as* out when I found it
0
SaswatPadhi's Avatar, Join Date: May 2009
~ Б0ЯИ Τ0 С0δЭ ~
Lets make it interesting.
I request G4EF members to write the above mentioned program in ANY language of their choice, provided the same language has not been used by any other member in any of the previous posts in this thread.

Let me start with C
Code: c
#include <stdio.h>

int main()
{
      unsigned N, flag = 0;
      for( N = 1; N < 101; ++N, flag = 0 )
      {
            if ( ! (N % 3) )      {      printf("Fizz");      flag = 1;      }
            if ( ! (N % 5) )      {      printf("Buzz");      flag = 1;      }
            if ( ! flag )         printf("%d", N);
            putchar(10);
      }
      return 0;
}
0
navinkumarank's Avatar, Join Date: Nov 2008
Newbie Member
this is really cool... !

I have it here in perl

Code:
perl -le 'print (($_%15)?(($_%3)?(($_%5)?$_:"Buzz"):"Fizz"):"FizzBuzz") for 1..100'
OR
Code:
#!/usr/bin/perl

for (1..100){
    print (($_%15)?(($_%3)?(($_%5)?$_."\n":"Buzz\n"):"Fizz\n"):"FizzBuzz\n");
}
let me know ur views... !

Last edited by shabbir; 28May2009 at 10:03.. Reason: Code blocks
0
SaswatPadhi's Avatar, Join Date: May 2009
~ Б0ЯИ Τ0 С0δЭ ~
Please ALWAYS post code inside code-blocks. (I request shabbir to edit the above post.)

I would really wish to see the FizzBuzz program in some esoteric programming language
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Done
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Code: Cpp
for( unsigned N = 1; N < 101;N++,(!(N%15))?cout<<"FizzBuzz":(!(N%5))?cout<<"Buzz":(!(N%3))?cout<<"Fizz":cout<<N<<endl);return 0;
0
SaswatPadhi's Avatar, Join Date: May 2009
~ Б0ЯИ Τ0 С0δЭ ~
What was that shabbir ?!

Where is the main func ?
Writing the whole code in a single line, significantly reduces the readability of your code.
0
pradeep's Avatar, Join Date: Apr 2005
Team Leader
Mine in shell script

Code: sh
#!/bin/sh

for i in `seq 1 100`
do
    if [ `expr $i % 3`  -eq 0 ] && [ `expr $i % 5`  -eq 0 ];
    then
        echo "FizzBuzz"
    elif [ `expr $i % 3` -eq 0 ];
    then
        echo "Fizz"
    elif [ `expr $i % 5`  -eq 0 ];
    then
        echo "Buzz"
    else
        echo $i
    fi

done
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by SaswatPadhi View Post
What was that shabbir ?!

Where is the main func ?
Writing the whole code in a single line, significantly reduces the readability of your code.
Its same as your code only and so I thought would just not change the printf's to cout and so did it that way buddy