# FizzBuzz

rascal-0.33.7

#### Synopsis​

We solve a well-known job interview puzzle.

#### Description​

FizzBuzz is a well-known puzzle that is used at job interviews. It is defined as follows:

Write a program that prints the numbers from 1 to 100. But for multiples of 3 print "Fizz" instead of the number and for the multiples of 5 print "Buzz". For numbers which are multiples of both 3 and 5 print "FizzBuzz".

Surprisingly, many candidates fail to pass this test. Solutions to FizzBuzz in various languages are available here.

#### Examples​

Here are a few possible Rascal solutions:

``import IO; @synopsis{fizzbuzz1 revolves around ternary conditions}void fizzbuzz1() {   for (int n <- [1 .. 101]){      fb = ((n % 3 == 0) ? "Fizz" : "") + ((n % 5 == 0) ? "Buzz" : "");      println((fb == "") ?"<n>" : fb);   }}@synopsis{fizzbuzz2 embraces pattern matching and the switch statement}void fizzbuzz2() {  for (n <- [1..101])     switch(<n % 3 == 0, n % 5 == 0>) {      case <true,true>  : println("FizzBuzz");      case <true,false> : println("Fizz");      case <false,true> : println("Buzz");      default: println(n);    }} @synopsis{fizzbuzz3 uses classical structured if-then-else} void fizzbuzz3() {  for (n <- [1..101]) {    if (n % 3 == 0) {      print("Fizz");    }    if (n % 5 == 0) {      print("Buzz");    } else if (n % 3 != 0) {      print(n);    }    println("");  }}``
``rascal>fizzbuzz1();12Fizz4BuzzFizz78FizzBuzz11Fizz1314FizzBuzz1617Fizz19BuzzFizz2223FizzBuzz26Fizz2829FizzBuzz3132Fizz34BuzzFizz3738FizzBuzz41Fizz4344FizzBuzz4647Fizz49BuzzFizz5253FizzBuzz56Fizz5859FizzBuzz6162Fizz64BuzzFizz6768FizzBuzz71Fizz7374FizzBuzz7677Fizz79BuzzFizz8283FizzBuzz86Fizz8889FizzBuzz9192Fizz94BuzzFizz9798FizzBuzzok``