You are on page 1of 2

Perl Programming (IS TEC PERL)

Assignment 3 Control Structures


1) Write a program that asks for the room temperature (C), it prints too hot if the temperature is above 30C, too cold if the temperature is below 26C, and just right! if it is between 26C and 30C. Filename: perl3_1.pl 2) Write a program that asks the user to enter two odd numbers, creates an array that contains all odd numbers between them (including the entered numbers), and prints the numbers in the array in descending order. If the entered numbers are not odd, the program should only print an error message. If the first entered number is larger than the second one, the program should still be able to print all the odd numbers between them (and the numbers themselves) in descending order. (Hint: Use push, if and while/for loop) Filename: perl3_2.pl 3) Write a program that repeats asking the user to enter a number until the cumulative sum of the entered numbers exceeds 100 (or some other constant number set by the program). Use a do { } while loop. Filename: perl3_3.pl Advanced exercise (to be used later) 4) Write a program that ask user to enter his NRIC number (with the alphabet), and print an error message if the NRIC number is not valid. Follow these steps to validate the NRIC number: a) Multiply each digit in the NRIC number by its weight in table 1. b) Sum up all the products. c) Divide the resulting sum by 11, and note the remainder. d) Subtract the remainder from 11 to get the check digit. e) Map the check digit to the corresponding alphabet using table 2.

Digit position Weight Table 1: Check Digit Alphabet 1 A 2 B

1st 2

2nd 7

3rd 6

4th 5

5th 4

6th 3

7th 2

Weight of each digit in NRIC number 3 C 4 D 5 E 6 F 7 G 8 H 9 I 10 Z 11 J

Table 2: Example:

Conversion table for check digit

To compute the letter of NRIC No. 8512345: Step (a) & (b): (8 * 2) + (5 * 7) + (1 * 6) + (2 * 5) + (3 * 4) + (4 * 3) + (5 * 2) = 16 + 35 + 6 + 10 + 12 + 12 + 10 = 101 Step (c): 101 /11 = 9 remainder 2 Step (d): 11 2 = 9 Step (e): Alphabet is I from table 2.

Assumption: Assume that the user enter a 7-digit number followed by a letter. Filename: perl3_4.pl

You might also like