Answer to Question #306816 in C for bhuvaneshwar

Question #306816

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


1
Expert's answer
2022-03-06T14:04:18-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS