99 Bottles of Beer computer program
A 99 Bottles of Beer computer program is a common programming exercise. The idea is to program a routine writing out the full lyrics of "99 Bottles of Beer".
Example in C++:
int main() {
int i;
for(i = 99; i > 0; --i) {
cout i << " bottles of beer on the wall\n";
cout i << " bottles of beer!\n";
cout "Take one down, pass it around. " << (i-1) << " bottles of beer on the wall!\n";
}
return 0;
}
perl -e '$b="of beer";$w="on the wall";print "$_ ",b($_)," $b $w\n$_ ",b($_)," $b\ntake one down, pass it around, ",$_-1," ",b($_-1)," $b $w\n\n" foreach (reverse 1..99);sub b{$x="bottle";$_[0]==1?$x:$x."s";}'
Example in PHP:
<?PHP
for($I = 99; $I > 0; $I--)
{
echo $I . " bottles of beer on the wall!\n";
echo $I . " bottles of beer!\n";
echo "Take one down, pass it around. " . ($I – 1) . " bottles of beer on the wall!\n\n";
}
?>
Example in Common Lisp:
(defun bottles (i)
(format t "~d bottles of beer on the wall~%" i)
(format t "~d bottles of beer!~%" i)
(format t "take one down pass it around, ~d bottles of beer on the wall~%" (- i 1))
(if (< i 2)
t
(bottles (- i 1) )))
Related topics
External links
Categories: Programming | Computer terminology