FizzBuzz in 10 programming languages 07-07-2013, 09:04 PM
#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:
Perl:
Lua:
Ruby:
Batch
Python 2.7:
C:
C++:
I didn't think I still knew some C++.
Java:
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:
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.
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
endRuby:
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
endBatch
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 startPython 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 iC:
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
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


![[+]](https://sinister.li/images/modern/collapse_collapsed.png)

![[Image: 2YpkRjy.png]](http://i.imgur.com/2YpkRjy.png)