Snippets

sironekotoro Perl入学式 2018 第2回 練習問題 fizzbuzz.pl

Created by sironekotoro

File fizzbuzz.pl Added

  • Ignore whitespace
  • Hide word diff
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+# Perl入学式 2018 第2回 練習問題
+# https://github.com/perl-entrance-org/workshop-2018/blob/master/2nd/slide.md
+# 練習問題(fizzbuzz.pl)
+
+# FizzBuzz問題にチャレンジ!!
+#   1から100までの数字について, 以下のようなルールに従って表示を行うfizzbuzz.plを作成しよう!
+#     その数字が3で割り切れるならFizz
+#     5で割り切れるならBuzz
+#     3でも5でも割り切れるならFizzBuzz
+#     3でも5でも割り切れないならその数字
+
+my @numbers = ( 1 .. 100 );
+
+for my $number (@numbers) {
+    if ( $number % 3 == 0 && $number % 5 == 0 ) {
+        print "fizzbuzz\n";
+    }
+    elsif ( $number % 3 == 0 ) {
+        print "fizz\n";
+    }
+    elsif ( $number % 5 == 0 ) {
+        print "buzz\n";
+    }
+    else {
+        print "$number\n";
+    }
+}
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.