Ms. Ashwini has a deck of cards and she wants to find the Fibonacci series of chosen cards. Write an application using functions to find the Fibonacci series of chosen cards. (The Fibonacci numbers are generated by setting F0 = 0, F1 = 1, and then using the recursive formula. Fn = Fn-1 + Fn-2. to get the rest. Thus the sequence begins: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55.).
The requirements to solve the problem are
a: Get the number up to which Fibonacci series is to be calculated.
b: Write a function to calculate the Fibonacci series
c: Print the result
#include <stdio.h>
long Fibonacci(int n) {
long f0=0, f1=1, f;
int i;
if (n == 0) {
return f0;
}
if (n == 1) {
return f1;
}
for (i=1; i<n; i++) {
f = f0 + f1;
f0 = f1;
f1 = f;
}
return f;
}
int main() {
long f;
int n, i;
scanf("%d", &n);
for (i=0; i<n; i++) {
f = Fibonacci(i);
printf("%ld, ", f);
}
f = Fibonacci(n);
printf("%ld", f);
return 0;
}
Comments
Leave a comment