Department of Computer Science
The University of Hong Kong
CSIS1117A Computer Programming I
Assignment 3
Due Date: 23:59 Nov 1, 2009.
You may assume all inputs are valid in these exercises.
1.
The Fibonacci Sequence is defined by the following recurrence relation:
( )
()
(
)
0,
1
1,
2
12
fn=
i
f
n=
ifn =
otherwise
fn
+n
,
⎧
⎫
⎪
⎪
⎨
⎬
⎪
⎪
−−
⎩⎭
Assuming the first two Fibonacci number is 0 and 1. Their values are 0, 1, 1, 2, 3, 5, 8, 13, 21,
34, 55, 89, 144, 233, 377, 610, 987, 1597 .
..
Write a program to print the first
n
Fibonacci number (the output number is divided by a space).
Your program should read the integer value
n(n > 0)
from
cin
.
Sample run of your program (The line whose font is
Times New Roman, Italic and Bold
: is
user input; the line whose font is
Courier and Bold
is output):
8
0 1 1 2 3 5 8 13
2.
The Luhn algorithm is a simple checksum formula to prevent simple typo error when inputting a
sequence of digits, such as credit card numbers. The algorithm works as follows:
Initialize sum to 0.