PowerShell Fizz Buzz


Often in programming interviews the Fizz Buzz question is posed as a way to vet a candidate’s understanding of programming logic. Here is a PowerShell solution to this question.

<#
"Write a program that prints 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: 8". 
For numbers which are multiples of both three and five print FizzBuzz."
#>

for ($num=1; $num -le 100; $num++){
    If (($num % 3 -eq 0) -and ($num % 5 -eq 0)) {Write-Output "FizzBuzz: $num"}
    ElseIf ($num % 3 -eq 0) {Write-Output "Fizz: $num"}
    ElseIf ($num % 5 -eq 0) {Write-Output "Buzz: $num"}
    Else {$num}
}

Leave a Reply

Your email address will not be published. Required fields are marked *