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.
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
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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 ;