Login Register






FizzBuzz in 10 programming languages filter_list
Author
Message
FizzBuzz in 10 programming languages #1
The FizzBuzz program in 10 programming languages

Dude, what the heck is a FizzBuzz?

FizzBuzz is a game kids were pushed to play at school (I think in Britain, mainly). Each kid would say the successive number of the one said by the kid before him. If the number was a multiplier of 3, instead he would say "Fizz", if it was a multiplier of 5 "Buzz", if it was a multiplier of both "FizzBuzz". A FizzBuzz program is hence a program printing a range of numbers (usually from 1 to 100), unless the number is a multiplier of 3 or 5.

Languages used:

1. PHP
2. Perl
3. Lua
4. Ruby
5. Batch
6. Python 2.7
7. C
8. C++
9. Java
10. AWK

I think a FizzBuzz is a better example for a simple program in a language than an Hello, world! program. You can understand some basics about loops, integers and variables, other than strings.

PHP:

Code:
<?php for ($i=1;$i<=100;$i++) { if ($i%3 == 0 && $i%5 == 0) { echo "FizzBuzz"; } elseif ($i%3 == 0) { echo "Fizz"; } elseif ($i%5 == 0) { echo "Buzz"; } else { echo $i; } } ?>

Perl:

Code:
for ($i=1;$i<=100;$i++) { if ($i%3 == 0 && $i%5 == 0) { print "FizzBuzz\n"; } elsif ($i%3 == 0) { print "Fizz\n"; } elsif ($i%5 == 0) { print "Buzz\n"; } else { print $i . "\n"; } }

Lua:

Code:
for i=1,100,1 do if i%3 == 0 and i%5 == 0 then print("FizzBuzz") elseif i%3 == 0 then print("Fizz") elseif i%5 == 0 then print("Buzz") else print(i) end end

Ruby:

Code:
for i in 1..100 if i%3 == 0 and i%5 == 0 puts "FizzBuzz" elsif i%3 == 0 puts "Fizz" elsif i%5 == 0 puts "Buzz" else puts i end end

Batch

Code:
@echo off set i=0 :start set /a i+=1 set f= set b= set /a m=i%%3 set /a n=i%%5 if "%m%"=="0" set f=Fizz if "%n%"=="0" (set b=Buzz) else (if not defined f set f=%i%) echo %f%%b% if %i% GEQ 100 exit /b goto start

Python 2.7:

Code:
for i in xrange(1,101): if i%3 == 0 and i%5 == 0: print "FizzBuzz" elif i%3 == 0: print "Fizz" elif i%5 == 0: print "Buzz" else: print i

C:

Code:
int main(void) { int i; for (i=1;i<=100;i++) { if (i%3==0 && i%5==0) { printf("FizzBuzz\n"); } else if (i%3 == 0) { printf("Fizz\n"); } else if (i%5 == 0) { printf("Buzz\n"); } else { printf("%d\n",i); } } return 0; }

C++:

Code:
#include <iostream> using namespace std; int main () { int i; for (i=1;i<=100;i++) { if ( i % 3 == 0 && i % 5 == 0 ) cout << "FizzBuzz" << endl; else if ( i % 3 == 0 ) cout << "Fizz" << endl; else if ( i % 5 == 0 ) cout << "Buzz" << endl; else cout << i << endl; } return 0; }

I didn't think I still knew some C++.

Java:

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

As I developed my first Java program, I also developed my hate for Java, not liking the idea of putting codes by others in this page.

AWK:

Code:
{ for (i=1;i<=100;i++) { if (i%3 == 0 && i%5 == 0) { print "FizzBuzz"; } else if (i%3 == 0) { print "Fizz"; } else if (i%5 == 0) { print "Buzz"; } else { print i; } } }

If you're asking "Why AWK?", I was out of languages.

If anyone has got any suggestion on any code, let me know, some of these languages I know well enough just to make a FizzBuzz program.



The AWK code is the only that hasn't been tested yet as I don't have AWK installed. Even though I'm sure it's correct, if anyone could give it a brief shot and vouch this I'd be glad.
My Bitcoin address: 1AtxVsSSG2Z8JfjNy9KNFDUN6haeKr7LiP
Give me money by visiting www.google.com here: http://coin-ads.com/6Ol83U

If you want a Bitcoin URL shortener/advertiser, please, use this referral: http://coin-ads.com/register.php?refid=noize

Reply

FizzBuzz in 10 programming languages #2
The FizzBuzz program in 10 programming languages

Dude, what the heck is a FizzBuzz?

FizzBuzz is a game kids were pushed to play at school (I think in Britain, mainly). Each kid would say the successive number of the one said by the kid before him. If the number was a multiplier of 3, instead he would say "Fizz", if it was a multiplier of 5 "Buzz", if it was a multiplier of both "FizzBuzz". A FizzBuzz program is hence a program printing a range of numbers (usually from 1 to 100), unless the number is a multiplier of 3 or 5.

Languages used:

1. PHP
2. Perl
3. Lua
4. Ruby
5. Batch
6. Python 2.7
7. C
8. C++
9. Java
10. AWK

I think a FizzBuzz is a better example for a simple program in a language than an Hello, world! program. You can understand some basics about loops, integers and variables, other than strings.

PHP:

Code:
<?php for ($i=1;$i<=100;$i++) { if ($i%3 == 0 && $i%5 == 0) { echo "FizzBuzz"; } elseif ($i%3 == 0) { echo "Fizz"; } elseif ($i%5 == 0) { echo "Buzz"; } else { echo $i; } } ?>

Perl:

Code:
for ($i=1;$i<=100;$i++) { if ($i%3 == 0 && $i%5 == 0) { print "FizzBuzz\n"; } elsif ($i%3 == 0) { print "Fizz\n"; } elsif ($i%5 == 0) { print "Buzz\n"; } else { print $i . "\n"; } }

Lua:

Code:
for i=1,100,1 do if i%3 == 0 and i%5 == 0 then print("FizzBuzz") elseif i%3 == 0 then print("Fizz") elseif i%5 == 0 then print("Buzz") else print(i) end end

Ruby:

Code:
for i in 1..100 if i%3 == 0 and i%5 == 0 puts "FizzBuzz" elsif i%3 == 0 puts "Fizz" elsif i%5 == 0 puts "Buzz" else puts i end end

Batch

Code:
@echo off set i=0 :start set /a i+=1 set f= set b= set /a m=i%%3 set /a n=i%%5 if "%m%"=="0" set f=Fizz if "%n%"=="0" (set b=Buzz) else (if not defined f set f=%i%) echo %f%%b% if %i% GEQ 100 exit /b goto start

Python 2.7:

Code:
for i in xrange(1,101): if i%3 == 0 and i%5 == 0: print "FizzBuzz" elif i%3 == 0: print "Fizz" elif i%5 == 0: print "Buzz" else: print i

C:

Code:
int main(void) { int i; for (i=1;i<=100;i++) { if (i%3==0 && i%5==0) { printf("FizzBuzz\n"); } else if (i%3 == 0) { printf("Fizz\n"); } else if (i%5 == 0) { printf("Buzz\n"); } else { printf("%d\n",i); } } return 0; }

C++:

Code:
#include <iostream> using namespace std; int main () { int i; for (i=1;i<=100;i++) { if ( i % 3 == 0 && i % 5 == 0 ) cout << "FizzBuzz" << endl; else if ( i % 3 == 0 ) cout << "Fizz" << endl; else if ( i % 5 == 0 ) cout << "Buzz" << endl; else cout << i << endl; } return 0; }

I didn't think I still knew some C++.

Java:

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

As I developed my first Java program, I also developed my hate for Java, not liking the idea of putting codes by others in this page.

AWK:

Code:
{ for (i=1;i<=100;i++) { if (i%3 == 0 && i%5 == 0) { print "FizzBuzz"; } else if (i%3 == 0) { print "Fizz"; } else if (i%5 == 0) { print "Buzz"; } else { print i; } } }

If you're asking "Why AWK?", I was out of languages.

If anyone has got any suggestion on any code, let me know, some of these languages I know well enough just to make a FizzBuzz program.



The AWK code is the only that hasn't been tested yet as I don't have AWK installed. Even though I'm sure it's correct, if anyone could give it a brief shot and vouch this I'd be glad.
My Bitcoin address: 1AtxVsSSG2Z8JfjNy9KNFDUN6haeKr7LiP
Give me money by visiting www.google.com here: http://coin-ads.com/6Ol83U

If you want a Bitcoin URL shortener/advertiser, please, use this referral: http://coin-ads.com/register.php?refid=noize

Reply

FizzBuzz in 10 programming languages #3
The FizzBuzz program in 10 programming languages

Dude, what the heck is a FizzBuzz?

FizzBuzz is a game kids were pushed to play at school (I think in Britain, mainly). Each kid would say the successive number of the one said by the kid before him. If the number was a multiplier of 3, instead he would say "Fizz", if it was a multiplier of 5 "Buzz", if it was a multiplier of both "FizzBuzz". A FizzBuzz program is hence a program printing a range of numbers (usually from 1 to 100), unless the number is a multiplier of 3 or 5.

Languages used:

1. PHP
2. Perl
3. Lua
4. Ruby
5. Batch
6. Python 2.7
7. C
8. C++
9. Java
10. AWK

I think a FizzBuzz is a better example for a simple program in a language than an Hello, world! program. You can understand some basics about loops, integers and variables, other than strings.

PHP:

Code:
<?php for ($i=1;$i<=100;$i++) { if ($i%3 == 0 && $i%5 == 0) { echo "FizzBuzz"; } elseif ($i%3 == 0) { echo "Fizz"; } elseif ($i%5 == 0) { echo "Buzz"; } else { echo $i; } } ?>

Perl:

Code:
for ($i=1;$i<=100;$i++) { if ($i%3 == 0 && $i%5 == 0) { print "FizzBuzz\n"; } elsif ($i%3 == 0) { print "Fizz\n"; } elsif ($i%5 == 0) { print "Buzz\n"; } else { print $i . "\n"; } }

Lua:

Code:
for i=1,100,1 do if i%3 == 0 and i%5 == 0 then print("FizzBuzz") elseif i%3 == 0 then print("Fizz") elseif i%5 == 0 then print("Buzz") else print(i) end end

Ruby:

Code:
for i in 1..100 if i%3 == 0 and i%5 == 0 puts "FizzBuzz" elsif i%3 == 0 puts "Fizz" elsif i%5 == 0 puts "Buzz" else puts i end end

Batch

Code:
@echo off set i=0 :start set /a i+=1 set f= set b= set /a m=i%%3 set /a n=i%%5 if "%m%"=="0" set f=Fizz if "%n%"=="0" (set b=Buzz) else (if not defined f set f=%i%) echo %f%%b% if %i% GEQ 100 exit /b goto start

Python 2.7:

Code:
for i in xrange(1,101): if i%3 == 0 and i%5 == 0: print "FizzBuzz" elif i%3 == 0: print "Fizz" elif i%5 == 0: print "Buzz" else: print i

C:

Code:
int main(void) { int i; for (i=1;i<=100;i++) { if (i%3==0 && i%5==0) { printf("FizzBuzz\n"); } else if (i%3 == 0) { printf("Fizz\n"); } else if (i%5 == 0) { printf("Buzz\n"); } else { printf("%d\n",i); } } return 0; }

C++:

Code:
#include <iostream> using namespace std; int main () { int i; for (i=1;i<=100;i++) { if ( i % 3 == 0 && i % 5 == 0 ) cout << "FizzBuzz" << endl; else if ( i % 3 == 0 ) cout << "Fizz" << endl; else if ( i % 5 == 0 ) cout << "Buzz" << endl; else cout << i << endl; } return 0; }

I didn't think I still knew some C++.

Java:

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

As I developed my first Java program, I also developed my hate for Java, not liking the idea of putting codes by others in this page.

AWK:

Code:
{ for (i=1;i<=100;i++) { if (i%3 == 0 && i%5 == 0) { print "FizzBuzz"; } else if (i%3 == 0) { print "Fizz"; } else if (i%5 == 0) { print "Buzz"; } else { print i; } } }

If you're asking "Why AWK?", I was out of languages.

If anyone has got any suggestion on any code, let me know, some of these languages I know well enough just to make a FizzBuzz program.



The AWK code is the only that hasn't been tested yet as I don't have AWK installed. Even though I'm sure it's correct, if anyone could give it a brief shot and vouch this I'd be glad.
My Bitcoin address: 1AtxVsSSG2Z8JfjNy9KNFDUN6haeKr7LiP
Give me money by visiting www.google.com here: http://coin-ads.com/6Ol83U

If you want a Bitcoin URL shortener/advertiser, please, use this referral: http://coin-ads.com/register.php?refid=noize

Reply

RE: FizzBuzz in 10 programming languages #4
Use Haskell. Or Prolog. Wink

The Java code doesn't adhere the coding conventions. Class names are uppercase. Java also has the ++ operator, btw.

The main function of the C code needs a return statement. Making i a global variable is not necessary. Stay as local as possible.

Starting loops at 1 is odd.

For loops in python should always use xrange instead of range.
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: FizzBuzz in 10 programming languages #5
Use Haskell. Or Prolog. Wink

The Java code doesn't adhere the coding conventions. Class names are uppercase. Java also has the ++ operator, btw.

The main function of the C code needs a return statement. Making i a global variable is not necessary. Stay as local as possible.

Starting loops at 1 is odd.

For loops in python should always use xrange instead of range.
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: FizzBuzz in 10 programming languages #6
Use Haskell. Or Prolog. Wink

The Java code doesn't adhere the coding conventions. Class names are uppercase. Java also has the ++ operator, btw.

The main function of the C code needs a return statement. Making i a global variable is not necessary. Stay as local as possible.

Starting loops at 1 is odd.

For loops in python should always use xrange instead of range.
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: FizzBuzz in 10 programming languages #7
(07-07-2013, 09:38 PM)Deque Wrote: Use Haskell. Or Prolog. Wink

The Java code doesn't adhere the coding conventions. Class names are uppercase. Java also has the ++ operator, btw.

The main function of the C code needs a return statement. Making i a global variable is not necessary. Stay as local as possible.

Starting loops at 1 is odd.

For loops in python should always use xrange instead of range.

How come I didn't think of Haskell instead of going for Java, which, as it appears, I don't know at all. I don't really know Prolog.

I'll change the class names (only one llitter's got to be uppercase, right?), though, it works this way too, is there any actual difference or is it just convention?

The main function in C does *not* need a return statement: it's a void function. For the variable, yes, you're right.

Why is it odd to start a loop at 1? :huh:

I'm not good with Python, you know, may I get a brief reason for what you're argueing?

Going to make some edits.

Edit: you know Prolog too?

Edit: I knew Java had the ++ operator, I thought I had used it.
My Bitcoin address: 1AtxVsSSG2Z8JfjNy9KNFDUN6haeKr7LiP
Give me money by visiting www.google.com here: http://coin-ads.com/6Ol83U

If you want a Bitcoin URL shortener/advertiser, please, use this referral: http://coin-ads.com/register.php?refid=noize

Reply

RE: FizzBuzz in 10 programming languages #8
(07-07-2013, 09:38 PM)Deque Wrote: Use Haskell. Or Prolog. Wink

The Java code doesn't adhere the coding conventions. Class names are uppercase. Java also has the ++ operator, btw.

The main function of the C code needs a return statement. Making i a global variable is not necessary. Stay as local as possible.

Starting loops at 1 is odd.

For loops in python should always use xrange instead of range.

How come I didn't think of Haskell instead of going for Java, which, as it appears, I don't know at all. I don't really know Prolog.

I'll change the class names (only one llitter's got to be uppercase, right?), though, it works this way too, is there any actual difference or is it just convention?

The main function in C does *not* need a return statement: it's a void function. For the variable, yes, you're right.

Why is it odd to start a loop at 1? :huh:

I'm not good with Python, you know, may I get a brief reason for what you're argueing?

Going to make some edits.

Edit: you know Prolog too?

Edit: I knew Java had the ++ operator, I thought I had used it.
My Bitcoin address: 1AtxVsSSG2Z8JfjNy9KNFDUN6haeKr7LiP
Give me money by visiting www.google.com here: http://coin-ads.com/6Ol83U

If you want a Bitcoin URL shortener/advertiser, please, use this referral: http://coin-ads.com/register.php?refid=noize

Reply

RE: FizzBuzz in 10 programming languages #9
(07-07-2013, 09:38 PM)Deque Wrote: Use Haskell. Or Prolog. Wink

The Java code doesn't adhere the coding conventions. Class names are uppercase. Java also has the ++ operator, btw.

The main function of the C code needs a return statement. Making i a global variable is not necessary. Stay as local as possible.

Starting loops at 1 is odd.

For loops in python should always use xrange instead of range.

How come I didn't think of Haskell instead of going for Java, which, as it appears, I don't know at all. I don't really know Prolog.

I'll change the class names (only one llitter's got to be uppercase, right?), though, it works this way too, is there any actual difference or is it just convention?

The main function in C does *not* need a return statement: it's a void function. For the variable, yes, you're right.

Why is it odd to start a loop at 1? :huh:

I'm not good with Python, you know, may I get a brief reason for what you're argueing?

Going to make some edits.

Edit: you know Prolog too?

Edit: I knew Java had the ++ operator, I thought I had used it.
My Bitcoin address: 1AtxVsSSG2Z8JfjNy9KNFDUN6haeKr7LiP
Give me money by visiting www.google.com here: http://coin-ads.com/6Ol83U

If you want a Bitcoin URL shortener/advertiser, please, use this referral: http://coin-ads.com/register.php?refid=noize

Reply

RE: FizzBuzz in 10 programming languages #10
(07-07-2013, 09:59 PM)noize Wrote: I'll change the class names (only one llitter's got to be uppercase, right?), though, it works this way too, is there any actual difference or is it just convention?

Convention only. http://www.oracle.com/technetwork/java/c...38413.html

To be exact, this is what the conventions say:

Quote:Class names should be nouns, in mixed case with the first letter of each internal word capitalized

Quote:The main function in C does *not* need a return statement: it's a void function. For the variable, yes, you're right.

Wrong. You even declared it as int main. You have to return the status code, 0 for success, another integer if an error happened.
Some compilers will allow you to have a void main, but that's against the standard of the language and you should avoid it at all costs.

Quote:Why is it odd to start a loop at 1? :huh:

Because most programmers do:
for i = 0; i < 100; i++
But actually I got it wrong here, as the FizzBuzz test states it should go from 1 to 100.

Quote:I'm not good with Python, you know, may I get a brief reason for what you're argueing?

range() creates a list that is saved in memory. xrange() is more like a generator. The numbers are only created when needed.

Quote:Edit: you know Prolog too?

Yes, I took two AI courses and there we used Prolog. It's a relatively easy language in the sense that it uses one algorithm for resolution and once you understood it, there isn't much else to know. (One of my professors wrote an interpreter for prolog in Haskell within a single lecture.)
But you need a very good understanding of mathematical logic. It is a logic programming language.
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply