Home | Lecture Notes | PDF slides | Next


Perl 2007: Exercises 1

These exercises are part of a programming course taught at University of Tilburg, The Netherlands.

If you are a course participant, please send the solutions to these exercises to erikt(at)science.uva.nl before or on Tuesday September 11, 2007. Note that the only the first three exercises are obligatory.

Exercise 1.1

In the lecture notes we put forward the following age calculator:

   # age calculator
   print "Please enter your birth year ";
   $yearOfBirth = <STDIN>;
   print "Your age is ",2007-$yearOfBirth,".\n";

Use the editor for storing this program in a file and run it. Enter birth years of three different people and write down the program's output. Will the program always get it right?

[Solution] Only look at the solution when you have finished the exercise or when you are completely stuck.

Exercise 1.2

The program of exercise 1.1 contains a problem: it will only show the correct age if the user has already celebrated his birthday this year. Modify the program so that it produces output like:

   Your age is 24 or 23.

One of the two numbers should represent the correct age (first the highest value and then the lowest).

Exercise 1.3

Implement the following instructions in a Perl program. All parts should be represented by a command in your program.

Read a three digit number (ask for the first digit being different from the last). Create three separate variables containing the first, second and third digit of the number. Create a variable containing the reverse of the number (digit order: three, two, one). Subtract this number from the first. Take the absolute value of the result. Create a variable holding the three-digit reverse of this absolute number. Add the new number to the absolute number.

The program should read an arbitrary number, perform the instructions and print the result. Test your program on three different numbers. What results do you obtain?

Exercise 1.4*

This is a starred exercise which means that you may skip the exercise. Make this exercise only if you think it is interesting and if you have some time left.

Modify the weekday calculator presented in section 1.4 of the lecture notes in such a way that it correctly handles dates from the year 2008 instead of 2007.

Note: you cannot use this exercise for upgrading your mark!

Exercise 1.5*

Write a Perl program that reads two different numbers and prints the largest one. The program may only use Perl instructions which were explained in lesson 1.

Note: you cannot use this exercise for upgrading your mark!

Home | Lecture Notes | PDF slides | Next
Last update: October 23, 2007. erikt(at)science.uva.nl