FIZZBUZZ
A program that prints (to STDOUT) 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”.PROGRAM OUTPUT
#include <iostream>
using namespace std;
int main()
{
for(int i=1;i<=100;i++){
if(i%15 == 0)
cout<<"FizzBuzz\n";
else if(i%3 == 0)
cout<<"Fizz\n";
else if(i%5 == 0)
cout<<"Buzz\n";
else
cout<<i<<"\n";
}
return 0;
}
}
1
2
Fizz
4
Buzz
Fizz
7
.
.
14
FizzBuzz
.
.
.
.
.
.
Fizz
100
Shortest code in perl
for(1..100){$x=!($_%5);$y=!($_%3);$_=($y?"Fizz":"").($x?"Buzz":"")if($x+$y);print;print "\n";}
No comments:
Post a Comment