Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Programming (http://www.go4expert.com/forums/programming-forum/)
-   -   FizzBuzz Test (http://www.go4expert.com/forums/fizzbuzz-test-t17716/)

pradeep 27May2009 17:51

FizzBuzz Test
 
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.

SaswatPadhi 27May2009 19:19

Re: FizzBuzz Test
 
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;
}


navinkumarank 28May2009 01:12

Re: FizzBuzz Test
 
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... !

SaswatPadhi 28May2009 09:40

Re: FizzBuzz Test
 
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 :wink:

shabbir 28May2009 10:03

Re: FizzBuzz Test
 
Done

shabbir 28May2009 10:23

Re: FizzBuzz Test
 
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;


SaswatPadhi 28May2009 12:11

Re: FizzBuzz Test
 
:eek: :surprised What was that shabbir ?!

Where is the main func ?
Writing the whole code in a single line, significantly reduces the readability of your code.

pradeep 28May2009 12:52

Re: FizzBuzz Test
 
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


shabbir 28May2009 13:36

Re: FizzBuzz Test
 
Quote:

Originally Posted by SaswatPadhi (Post 48528)
:eek: :surprised 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

xpi0t0s 28May2009 13:51

Re: FizzBuzz Test
 
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 ;



All times are GMT +5.5. The time now is 22:32.