Вы находитесь на странице: 1из 2

ITM352 Exam 1 -- Programming PHP

Practice Questions
NOTES: For all questions, you should create a working, tested, commented PHP
program. All output should be nicely formatted.

1. Leap Year Calculation: Given a variable called $year, create a program that will print
out “<xxxx> is a leap year” or “<xxxx> is not a leap year” (where <xxxx> is the value of
$year). Use the following rules to determine if a year is a leap year: if the year is
divisible by 4 then it is a leap year, unless it is also divisible by 100 (in which case it is
not a leap year), unless it is divisible by 400 (in which case it is a leap year).

2. “Mod” calculation: The mod (%) operator in PHP calculates the remainder when $a
is divided by $b. Create a program that calculates the remainder by repeatedly
subtracting $b from $a until it can no longer be subtracted (i.e. stop subtracting when $a
is less than $b). Print out the remainder, nicely formatted (e.g. if $a=7 and $b=2 you
might print “7 mod 2 = 1”).

3. Evens, Odds, and Dozens: Create a program that will count down to 0 from a given
number—$start. For each number, print the number and then print (“ is even”) if it is
even and (“ is odd”), if it is odd. In addition, if the number is a multiple of 12, print the
number of dozens that it represents. For example if $start=27, the output might look like:

27 is odd.
26 is even.
25 is odd.
24 is even. 24 is 2 dozen.
23 is odd.

4. Making change: Create a program that, given a number of cents owing, will calculate
the change from one dollar. Make sure that the program calculates the minimum number
of quarters, dimes, nickels, and pennies. Also, make sure that the program checks that
the amount owing is between 1 cent and 99 cents (otherwise print a message like “Invalid
amount” and quit).

5. Reverse an array: Create a program that will take an array, called $the_array, and
create a reversed version of it, called $reverse. For example, if $the_array originally
contains {1, 0, 5, 12, 8}, your program should create $reverse containing {8, 12, 5, 0, 1}.
Don’t use any built-in PHP functions.

6. Compare Arrays: Compare a given array, called $the_array, with a possibly reversed
version, called $reverse. Output “True” if it is a correct reversal and output “False”
otherwise. Don’t use any built-in PHP functions.
7. Fibonacci Series: Create a program that will print out a Fibonacci series, e.g.
1 1 2 3 5 8 13 21 34 …

8. Mean, Max, Min: Create a program that, given an array called $the_array, will
calculate and output its mean, largest, and smallest values.

9. Euclid’s Algorithm: Create a program that will implement Euclid’s GCD (Greatest
Common Divisor) algorithm:

Assume you wish to find the GCD of 2047 and 391.

1. Divide the larger by the smaller and note the remainder: 2047/391 = (391 X 5) + 92

2. Divide the remainder (92) into the previous divisor (391): 391/92 = (92 X 4) + 23

3. Repeat steps 1 and 2 until the remainder is 1 or zero.

3a Divide the remainder (23) into the previous divisor (92): 92/23 = (23 X 4) + 0

4. When the remainder is zero the last divisor is the GCD! 23 X 89 =2047 and 23 X 17 =
391. Therefore 89/17 = 2047/391

5. When the remainder is 1 the two numbers have NO common divisor and are relatively
prime. Example: Assume you wish to find the GCD of 8191 and 1023.

8191/1023 = (1023 X 8) + 7

1023/7 = (7 X 146) + 1

The remainder is 1 therefore these two numbers have NO common divisor!

Вам также может понравиться