Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Fizz-Buzz: The Multi-Language Challenge
Index -> General Programming
Goto page 1, 2, 3, 4  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
haskell




PostPosted: Sat Mar 24, 2007 1:44 pm   Post subject: Fizz-Buzz: The Multi-Language Challenge

Task

To write the same program in as many languages as possible.

Purpose

So people can see the same code written in as many languages as possible.

The Program

Count from 1-100 and write the results. If the value is divisible by 3, than write "Fizz". If the value is divisible by 5, than write "Buzz". If the value is divisible by both 3 and 5, than write "FizzBuzz".

Rules

State the language. Multiple solutions in the same language is acceptable. Use any means necessary Smile.

Enjoy Very Happy

---

C++
code:
string fizz_buzz;

for(int i = 1; i <= 100; i++) {
    if(i % 3 == 0) fizz_buzz += "Fizz";
    if(i % 5 == 0) fizz_buzz += "Buzz";
    if(fizz_buzz == "") cout << i;
    cout << fizz_buzz << endl;
    fizz_buzz = "";
}
Sponsor
Sponsor
Sponsor
sponsor
ericfourfour




PostPosted: Sat Mar 24, 2007 2:02 pm   Post subject: Re: Fizz-Buzz: The Multi-Language Challenge

Turing:
View.Set("text")

var fizzBuzz : string := ""

for i : 1 .. 100
    if i mod 3 = 0 then
        fizzBuzz += "Fizz"
    end if
    if i mod 5 = 0 then
        fizzBuzz += "Buzz"
    end if
    if fizzBuzz = "" then
        put i ..
    end if
    put fizzBuzz
    fizzBuzz := ""
end for
rdrake




PostPosted: Sat Mar 24, 2007 2:02 pm   Post subject: RE:Fizz-Buzz: The Multi-Language Challenge

I think this is what it should do...
code:
fizz_buzz = ""

100.times do |i|
    if (i % 3) == 0 then fizz_buzz += "Fizz" end
    if (i % 5) == 0 then fizz_buzz += "Buzz" end
    if fizz_buzz == "" then puts i end
    puts fizz_buzz
    fizz_buzz = ""
end
wtd




PostPosted: Sat Mar 24, 2007 2:11 pm   Post subject: RE:Fizz-Buzz: The Multi-Language Challenge

code:
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

procedure Fizz_Buzz is
   function Evenly_Divisible_By(A, B : in Integer) return Boolean is
   begin
      return A mod B = 0;
   end Evenly_Divisible_By;

   Count : Integer;
begin
   for Count in 1 .. 100
   loop
      if Evenly_Divisible_By(Count, 15) then
         Put_Line("FizzBuzz");
      elsif Evenly_Divisible_By(Count, 3) then
         Put_Line("Fizz");
      elsif Evenly_Divisible_By(Count, 5) then
         Put_Line("Buzz");
      else
         Put(Count, 0);
         New_Line;
      end if;
   end loop;
end Fizz_Buzz;
wtd




PostPosted: Sat Mar 24, 2007 2:22 pm   Post subject: RE:Fizz-Buzz: The Multi-Language Challenge

code:
class
   FIZZ_BUZZ

creation { ANY }
   make

feature { NONE }
   evenly_divisible_by(a, b : INTEGER) : BOOLEAN is
      do
         Result := a \\ b = 0
      end

feature { ANY }
   make is
      local
         count : INTEGER
      do   
         from
            count := 1
         until
            count > 100
         loop
            if evenly_divisible_by(count, 15) then
               std_output.put_string("FizzBuzz")
            elseif evenly_divisible_by(count, 3) then
               std_output.put_string("Fizz")
            elseif evenly_divisible_by(count, 5) then
               std_output.put_string("Buzz")
            else
               std_output.put_integer(count)
            end

            std_output.put_new_line
         end
      end
end
haskell




PostPosted: Sat Mar 24, 2007 2:32 pm   Post subject: RE:Fizz-Buzz: The Multi-Language Challenge

C
code:
int fizz_buzz(int, int);

int main()
{
    fizz_buzz(1, 100);   

    return 0;
}

int
fizz_buzz(int l, int h)
{
    if(l % 3 == 0) printf("Fizz");
    if(l % 5 == 0) printf("Buzz");
    else if(l % 3 != 0) printf("%d", l);
   
    printf("\n");

    if(l != h) return fizz_buzz(l + 1, h);
    else return 0;
}
wtd




PostPosted: Sat Mar 24, 2007 2:51 pm   Post subject: RE:Fizz-Buzz: The Multi-Language Challenge

code:
object Test extends Application {
   def evenlyDivisibleBy(a: Int, b: Int) = a % b == 0

   def fizzbuzz(a: Int, b: Int) {
      if (a <= b) {
         if (evenlyDivisibleBy(a, 15))
            Console println "FizzBuzz"
         else if (evenlyDivisibleBy(a, 3))
            Console println "Fizz"
         else if (evenlyDivisibleBy(a, 5))
            Console println "Buzz"
         else
            Console println a

         fizzbuzz(a + 1, b)
      }
   }

   fizzbuzz(1, 100)
}
PaulButler




PostPosted: Sat Mar 24, 2007 4:07 pm   Post subject: Re: Fizz-Buzz: The Multi-Language Challenge

In Java:

Java:

public class FizzBuzz {
        public static void main(String[] args) {
                for(int i = 1; i <= 100; i++){
                        String fb = "";
                        if(i % 3 == 0){
                                fb = "Fizz";
                        }
                        if(i % 5 == 0){
                                fb += "Buzz";
                        }
                        System.out.println(fb == ""? i : fb);
                }
        }
}
Sponsor
Sponsor
Sponsor
sponsor
PaulButler




PostPosted: Sat Mar 24, 2007 4:11 pm   Post subject: Re: Fizz-Buzz: The Multi-Language Challenge

php:

<?php

header('Content-type: text/plain');

for($i = 1; $i <= 100; $i++){
        $word = "";
        if($i % 3 == 0){
                $word = "Fizz";
        }
        if($i % 5 == 0){
                $word .= "Buzz";
        }
        echo ($word == "")? $i : $word;
        echo PHP_EOL;
}

?>
haskell




PostPosted: Sat Mar 24, 2007 4:21 pm   Post subject: RE:Fizz-Buzz: The Multi-Language Challenge

Python
code:
def fizz_buzz(x):
    fizz = ''
    if(x <= 100):
        if((x % 3) == 0):
            fizz += 'Fizz'
        if((x % 5) == 0):
            fizz += 'Buzz'
        elif((x % 3) != 0):
            fizz += str(x)
        print fizz
        fizz = ''
        fizz_buzz(x+1)

fizz_buzz(1)
wtd




PostPosted: Sat Mar 24, 2007 4:25 pm   Post subject: RE:Fizz-Buzz: The Multi-Language Challenge

I don't get why so many people have such apparent loathing for "else" and such irrational love of mutable state.
ericfourfour




PostPosted: Sat Mar 24, 2007 4:53 pm   Post subject: RE:Fizz-Buzz: The Multi-Language Challenge

wtd what were the programming languages you used?
wtd




PostPosted: Sat Mar 24, 2007 5:20 pm   Post subject: RE:Fizz-Buzz: The Multi-Language Challenge

I was hoping someone would try to guess...

Ada, Eiffel, and Scala.
richcash




PostPosted: Sat Mar 24, 2007 8:36 pm   Post subject: Re: Fizz-Buzz: The Multi-Language Challenge

In javascript in html :
Javascript:
<html>
<body>
<script type="text/javascript">
for (var i=1;i<=100;i++) {
        var output = ""
        if (i%3 == 0) {
                output += "Fizz"
        }
        if (i%5 == 0) {
                output += "Buzz"
        }
        if (output == "") {
                output = i
        }
        document.write(output)
        document.write("<br />")
}
</script>
</body>
</html>


P.S. Javascript is not a programming language, it's a scripting language.
wtd




PostPosted: Sun Mar 25, 2007 12:24 am   Post subject: RE:Fizz-Buzz: The Multi-Language Challenge

Javascript most certainly is a programming language. A darn good one, too.
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 4  [ 57 Posts ]
Goto page 1, 2, 3, 4  Next
Jump to:   


Style:  
Search: