# exercise 2.1 by erikt(at)science.uva.nl SOFTWARE # read a number print "Please enter a number: "; $nbr = ; chomp($nbr); # print a multiplication table with for for ($i=1;$i<=12;$i++) { # print an extra space before numbers under 10 to achieve alignment if ($i < 10) { $s = " "; } else { $s = ""; } print "$nbr * $s$i = ",$nbr*$i,"\n"; } print "\n"; # extra newline # print a multiplication table with foreach foreach $i (1,2,3,4,5,6,7,8,9,10,11,12) { # print an extra space before numbers under 10 to achieve alignment if ($i < 10) { $s = " "; } else { $s = ""; } print "$nbr * $s$i = ",$nbr*$i,"\n"; } print "\n"; # extra newline # print a multiplication table with while $i = 1; while ($i <= 12) { # print an extra space before numbers under 10 to achieve alignment if ($i < 10) { $s = " "; } else { $s = ""; } print "$nbr * $s$i = ",$nbr*$i,"\n"; $i++; } TEST erikt@stuwww:~$ perl -w 21.pl Please enter a number: 23 23 * 1 = 23 23 * 2 = 46 23 * 3 = 69 23 * 4 = 92 23 * 5 = 115 23 * 6 = 138 23 * 7 = 161 23 * 8 = 184 23 * 9 = 207 23 * 10 = 230 23 * 11 = 253 23 * 12 = 276 23 * 1 = 23 23 * 2 = 46 23 * 3 = 69 23 * 4 = 92 23 * 5 = 115 23 * 6 = 138 23 * 7 = 161 23 * 8 = 184 23 * 9 = 207 23 * 10 = 230 23 * 11 = 253 23 * 12 = 276 23 * 1 = 23 23 * 2 = 46 23 * 3 = 69 23 * 4 = 92 23 * 5 = 115 23 * 6 = 138 23 * 7 = 161 23 * 8 = 184 23 * 9 = 207 23 * 10 = 230 23 * 11 = 253 23 * 12 = 276