FizzBuzz Test

Team Leader
27May2009,17:51   #1
pradeep's Avatar
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.
~ Б0ЯИ Τ0 С0δЭ ~
27May2009,19:19   #2
SaswatPadhi's Avatar
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;
}
Newbie Member
28May2009,01:12   #3
navinkumarank's Avatar
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ЯИ Τ0 С0δЭ ~
28May2009,09:40   #4
SaswatPadhi's Avatar
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
Go4Expert Founder
28May2009,10:03   #5
shabbir's Avatar
Done
Go4Expert Founder
28May2009,10:23   #6
shabbir's Avatar
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ЯИ Τ0 С0δЭ ~
28May2009,12:11   #7
SaswatPadhi's Avatar
What was that shabbir ?!

Where is the main func ?
Writing the whole code in a single line, significantly reduces the readability of your code.
Team Leader
28May2009,12:52   #8
pradeep's Avatar
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
Go4Expert Founder
28May2009,13:36   #9
shabbir's Avatar
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
Mentor
28May2009,13:51   #10
xpi0t0s's Avatar
Forth (developed and tested with Win32Forth)
Code:
variable test3
variable test5
variable prt

: prog 1 0 0 test3 ! test5 ! prt !
20 1 do 
test3 @ 1 + dup test3 ! 3 = if ." fizz" 0 0 prt ! test3 ! then
test5 @ 1 + dup test5 ! 5 = if ." buzz" 0 0 prt ! test5 ! then
prt @ if i . else ."  " then
1 prt !
loop ;