2.12 Exercises

1. (Easy: Internet Exploration, Documentation Reading)
Either visit www.cpan.org on the World Wide Web, or use perldoc on your system to read documentation on Perl operators and basic syntax. Use

%perldoc perldoc

to learn more about perldoc. Detailed documentation on Perl operators is available by running

%perldoc perlop

Use perlnumber as the argument to find documentation on Perl numbers and numeric operators. Use perlsyn as the argument to find documentation on basic Perl syntax.
2. (Easy: Indefinite Iteration)
Write a while loop that prints a table of angle measures along with their sine and cosine values. The initial degree, the final degree and the step size are given as command-line arguments. The Perl built-in functions to compute sine and cosine are sin and cos, respectively. Each takes a radian argument.
3. (Easy: Iteration)
Write a program that takes a command-line input n and computes

n+(n-1)+(n-2)++2+1

using iteration. Rewrite the program so that it uses recursion. n is a positive integer. Check for errors and print error messages when needed.
4. (Easy: Recursion)
Write a program that prints the nth Fibonacci number fibonacci(n). n is given as a command-line argument. A Fibonacci number is defined in the following way.

fibonacci(0) = 1
fibonacci(1) = 1
fibonacci(n) = fibonacci(n-1)+fibonacci(n-2)n>=2
First write the program recursively. Then, write it iteratively. What are some of the salient differences between the two versions? Which version is better and why? Check for errors and print error messages when needed.
5. (Easy: Recursion, Iteration)
Write a program that computes n! recursively. It takes n as a command-line argument. Write a version where it is computed non-recursively. Which version is more efficient? In what way? Check for errors and print error messages when needed.
6. (Easy: Arithmetic)
Write a program that determines the day number (1 to 366) in year for a date that is provided as input data. As an example, January 1, 2000 is day 1. December 31, 1999 is day 365. December 31, 2004 is day 366 because 2004 is a leap year. A leap year is divisible by 4, and any year divisible by 100 is a leap year if it is divisible by 400. The program takes month, day and year as integers and on the command-line. Perform error checks as needed.
7. (Medium: Arithmetic, Formatting(
Write a program to generate a calendar for a year. The program takes the year and the day of the week for January 1 of that year. Assume 1 is Sunday, 2 is Monday, etc. Remember February has 29 days if it is a leap year.
8. (Medium: Binary Arithmetic, Hexadecimal Arithmetic)
Write a program itob that takes an argument: n. n is an unsigned binary integer. It returns a binary character representation of n. itob takes the integer as command-line argument.
Write itoh that converts an integer into hexadecimal representation.
9. (Easy: Infinite Loop, Temperature Conversion)
Write a program that has a while loop that runs for ever. Inside the loop, it prompts the user for a Fahrenheit temperature to convert to Celsius. If the user does not enter a valid number at the prompt, the program warns the user. It warns up to three times and then dies. Otherwise, it continues with the loop.
10. (Easy: Loop, File Copying)
Write a program that repeatedly asks for a file name in each iteration of the loop. If the user enters the name of a file that does not exist, it warns the user. If the user enters a valid file name, it copies the original file to a new file which has the same name except the string .copy at the end. Therefore, the file a.pl is copied into a.pl.copy. The program also warns if the file is not readable by the user or it is a directory.
11. (Easy: Arithmetic, Loops)
Write a program that takes n as a command-line argument and computes the following sum.

Sni=1Sij=1j2

Use two labeled loops. Use last, next and any other loop control operators to make the loops work. Do not use straight-forward looping statements.
12. (Easy: Loops, Arithmetic)
Write while, until, or for loops to computer the following. Try to write foreach loops also if you can.
(a) 1+3+7+◊◊◊+(220-1)
(b) 1¥2¥4¥8¥◊◊◊220
(c) ex=1+x+ x2!+◊◊◊
e is an important constant in mathematics. The formula given above is also very important in mathematics. Stop the iteration in the program when adding the next term changes the sum by less than 10-n. Here x and n are command-line arguments.
(d) 1+(1+4)+(1+4+7)+(1+4+7+10)+
Add this sequence to n terms where n is a command-line argument. Note that each term of the sequence is a sequence itself.
(e) 1- 12+ 13- 14+◊◊◊
Stop the iteration when the result is correct up to five decimal digits.
(f) sin x=x- x33!+ x55!+◊◊◊
Stop iteration after n decimal digit precision after the decimal point. x and n are command-line arguments.
13. (Easy to Hard: Prime Numbers, Arithmetic)
A prime number is an integer greater than one and divisible by one and itself. Write a Perl program that returns 1 if its command-line argument is a prime number.
Write another program that prints out all prime numbers below n where n is a command-line argument. Check for errors and print error messages when needed.
14. (Medium to Hard: Arithmetic)
A perfect number is a positive integer that is equal to the sum of its proper divisors. A proper divisor is a positive integer other than the number itself that divides the number without a remainder. For example, six is a perfect number because the sum of its proper divisors 1,2, and 3 equal 6. Eight is not a perfect number. Write a Perl program that takes a positive integer as command-line argument and determines whether it is perfect. Check for errors and print error messages when needed.