Here's a Perl5 one-liner:
$ perl -le 'print join " ", map { $_ % 15 ? $_ % 3 ? $_ % 5 ? $_ : "Fizz" : "Buzz" : "FizzBuzz" } (1..20)' 1 2 Buzz 4 Fizz Buzz 7 8 Buzz Fizz 11 Buzz 13 14 FizzBuzz 16 17 Buzz 19 Fizz
Note that your question states
any number divisible by 3 should be replaced by the word ‘fizz’
but the output shown has 3 and 5 interchanged
Ah.. Right. Thanks!
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Here's a Perl5 one-liner:
Note that your question states
but the output shown has 3 and 5 interchanged
Ah.. Right. Thanks!