forked from sgreenlee/C-Primer-Plus-Exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise11.c
54 lines (40 loc) · 896 Bytes
/
exercise11.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// C Primer Plus
// Chapter 9 Exercise 11:
// Write and test a Fibonacci() function that uses a loop instead of recursion
// to calculate Fibonacci numbers.
#include <stdio.h>
long Fibonacci(long n);
int main(void) {
long n;
printf("Test Fibonacci() function\n");
printf("Enter an integer n: ");
while (scanf("%ld", &n) == 1)
{
printf("Fibonacci #%ld = %ld\n", n, Fibonacci(n));
printf("Enter an integer n: ");
}
return 0;
}
long Fibonacci(long n)
{
// return nth Fibonacci number
// handle invalid arguments
if (n < 1)
{
printf("Error: n must be a positive integer.\n");
return -1;
}
long fib_n1 = 0, fib_n = 1, fib_n2;
// n equals 1 or 2
if (n == 1) return 0;
if (n == 2) return 1;
// n greater than or equal to 3
for (long i = 3; i <= n; i++)
{
// update old values
fib_n2 = fib_n1;
fib_n1 = fib_n;
fib_n = fib_n1 + fib_n2;
}
return fib_n;
}