Unformatted text preview: CSC 113, Fall 2010
Lab Assignment 4
1. Write a M ATLAB function ﬁle that computes the factorial of a positive integer. The factorial
of an integer N is
N ! = N ∗ (N − 1) ∗ (N − 2) ∗ · · · ∗ 2 ∗ 1
For example, 5! = 5 ∗ 4 ∗ 3 ∗ 2 ∗ 1 = 120 and, by convention, 0! = 1.
Your program should:
• Accept a single input scalar.
• Check to see if the input is nonnegative and integer valued. If not, send an error
message to the user and exit the program.
• If the input is valid, compute its factorial and return the value as the function’s output.
Do not use the M ATLAB factorial function in your program. The progrm might, however,
take advantage of the M ATLAB prod function (type help prod at the command prompt).
2. Write a M ATLAB function that accepts as its input a scalar value for age, and prints out a
message stating the privileges that go with the age:
• If the age is 16 or larger, the person can legally obtain a driver’s license.
• If the age is 18 or larger, the person is legally eligible to vote, and can legally sign
contracts, but might also be drafted.
• If the age is 21 or larger, the person can drink.
• If the age is 25 or larger, the person is allowed to rent a car.
• If the age is 45, the person is entitled to a midlife crisis.
• If the person is less than 16, display “Sorry, you’ll have to wait.”
The function should list all the privileges applicable at a given age. ...
View
Full Document
 Fall '10
 PhillipRegali
 Number Theory, Prime number, single input scalar

Click to edit the document details