print "Input a number: "; $Num = <>; chomp $Num; if (($Num % 2)==0) { print "That's divisible by 2!\n"; } if (($Num % 3)==0) { print "That's divisible by 3!\n"; } if (($Num % 4)==0) { print "That's divisible by 4!\n"; }
print "Input a number: "; $Num = <>; chomp $Num; if (($Num % 2)==0) { print "That's divisible by 2!\n"; } elsif (($Num % 3)==0) { print "That's divisible by 3!\n"; } elsif (($Num % 4)==0) { print "That's divisible by 4!\n"; }
print "Please input an animal: "; $Anim1 = <>; chomp $Anim1; print "Please input another animal: "; $Anim2 = <>; chomp $Anim2; if ($Anim1 gt $Anim2) { print "The $Anim1 ate the $Anim2!\n"; } else { print "The $Anim2 ate the $Anim1!\n"; }Describe the output of the program for the following pairs of inputs:
$Str = ""; $Count = ord("A"); while ($Count <= ord("G")) { $Let = chr($Count); $Str = "$Let$Str"; $Count++; } print "$Str\n";
$Count = 1; while ($Count<=10) { $CountSqr = $Count**2; print "$Count $CountSqr\n"; }
$Str = ""; for ($Count=0; $Count<10; $Count++) { $Str = "$Str$Count"; } print "$Str\n";
$Str = ""; for ($Count=0; $Count<10; $Count++) { $Str = "$Str$Count"; print "$Str\n"; }
[perkins] ls -l total 72 -rw-r--r-- 1 perkins perkins 117 Feb 9 10:47 DivBy3And4.pl -rw-r--r-- 1 perkins perkins 227 Feb 9 11:19 Divisible1.pl -rw-r--r-- 1 perkins perkins 208 Feb 9 11:23 Divisible2.pl -rw-r--r-- 1 perkins perkins 91 Feb 9 10:51 InfLoop.pl -rw-r--r-- 1 perkins perkins 267 Feb 9 11:15 Prime.pl -rw-r--r-- 1 perkins perkins 91 Feb 9 10:58 Print0to9.pl -rw-r--r-- 1 perkins perkins 92 Feb 9 11:00 Print0to9Triangle.pl -rw-r--r-- 1 perkins perkins 133 Feb 9 10:45 ReverseChars.pl -rw-r--r-- 1 perkins perkins 245 Feb 9 11:36 WhoAteWho.pl [perkins](a) What would be the result of the command "ls D*"?
Explanation: The program tests whether the input number is divisible by 2, 3 and 4. For each of those that divide the input number, it prints out.
Explanation: This program is similar to the previous one, but the second and third tests are "elsif" rather than just "if". That means, the conditions are tested only if the previous conditions have failed. So, for example, when the input is 2, the program finds that 2 is divisible by 2 and it never tests whether 2 is divisible by 3 or 4. Similarly when the input is 4 -- the program finds 4 is divisible by 2, and never tests either of the other conditions.
Explanation: Refer to the ASCII table to determine which strings are greater than which.
Explanation: The while loop is used to count the $Count variable from the ASCII index of "A" to the ASCII index of "G". For each of these indeces, the ASCII letter is added to the start of $Str. So, $Str starts as "", then after one time through the while loop is "A", then after another time is "BA", and so on. At the end, $Str is printed out.
1 1 1 1 1 1 1 1 1 1 (and so on...)Explanation: Although at first it might appear that the while loop is counting $Count from 1 to 10, $Count is not changed inside the while loop. (Ordinarily, we would have a line like "$Count++;" inside the while loop.) As such, $Count never changes from 1, and the while loop keeps repeating forever.
Explanation: The for loop steps $Count from 0 to 9. More precisely, it initially sets $Count to 0. After each time through the loop, it adds 1 to $Count, and it stops when $Count==10. Each time through the loop, $Count is added to the end of $Str, which thus ends up being 0123456789.
0 01 012 0123 01234 012345 0123456 01234567 012345678 0123456789Explanation: This program is very similar to the previous one, except that the string $Str is printed out each time through the for loop. In otherwords, it is print out when it is just "0", when it is "01", when it is "012", and so on.
for ($Count=1; $Count<=200; $Count++) { if ((($Count % 3)==0) && (($Count % 4)==0)) { print "$Count\n"; } }Explanation: The for loop counts from 1 to 200. The "if" checks if the number is divisible by 3 and by 4. If so, the number is printed out.
print "Please input a positive integer: "; $Num = <>; chomp $Num; $IsPrime = 1; for ($Count = 2; $Count<$Num; $Count++) { if (($Num % $Count)==0) { $IsPrime = 0; } } if ($IsPrime) { print "Yes, it is prime!\n"; } else { print "No, it is not prime!\n"; }Explanation: The program first takes the number as input. Then it sets $IsPrime to 1. This indicates that, as far as the program has checked so far, the input number is prime. The "for" loop counts from 2 to the number minus one. If it finds a number that divides the input number, it sets $IsPrime to 0. Thus, when the "for" loop ends, if no number was found that divides the input number, $IsPrime is still 1. Conversely, if a divisor was found for the input number, then $IsPrime is 0. The final "if" statement prints the correct output, depending on $IsPrime.
DivBy3And4.pl Divisible1.pl Divisible2.pl(b) This lists all files in the directory that have an "i" in them anywhere, producing:
DivBy3And4.pl Divisible2.pl Print0to9.pl Divisible1.pl Prime.pl Print0to9Triangle.pl(c) This lists all files in the directory beginning with "Pri" and puts the results in a new file called "NewFile.txt". The contents of that file is thus:
Prime.pl Print0to9.pl Print0to9Triangle.pl