28 lines
1.1 KiB
C
28 lines
1.1 KiB
C
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
|
||
|
unsigned long long fib(unsigned long long n) {
|
||
|
unsigned long long r; // Declare a local variable r
|
||
|
if (n == 0) { // If n is zero, return 0.
|
||
|
r = 0;
|
||
|
} else if (n == 1) { // If n is one, return 1.
|
||
|
r = 1;
|
||
|
} else { // Else, calculate the number
|
||
|
r = fib(n-1) + fib(n-2); // to return by utilising
|
||
|
} // recursion.
|
||
|
return r;
|
||
|
}
|
||
|
|
||
|
int main(int argc, char *argv[]) {
|
||
|
unsigned long long n, r; // Define local variables
|
||
|
n = atoll(argv[1]); // Convert the first argument
|
||
|
// to a long long value and
|
||
|
// load it into n.
|
||
|
r = fib(n); // Set the variable r to the
|
||
|
// n'th Fibonacci number.
|
||
|
// Print the number found.
|
||
|
printf("fib(%lld) = %lld\n", n, r);
|
||
|
return 0; // Return 0 to indicate a
|
||
|
// successful execution.
|
||
|
}
|