Answer to Question #328252 in C for Mominul

Question #328252

Suppose you are given a sequence of numbers as follows I, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144. .Now. you want to find the n number of the sequence i.e.. if you input n-7, program will give you output 13, for n=10, output=55. You MUST use dynamic programming technique to solve this problem and implement your solution in C language. What is the running time of your algorithm (write it in your text file).

1
Expert's answer
2022-04-13T11:25:35-0400

The running time of the program is O(n)

#include <stdio.h>
#include <stdlib.h>

int main() {
    int n, i;
    long *fib;

    printf("Enter a number: ");
    scanf("%d", &n);
    fib = (long*) malloc(n*sizeof(long));
    fib[0] = fib[1] = 1;

    for (i=2; i<n; i++) {
        fib[i] = fib[i-1] + fib[i-2];
    }


    printf("The %d-th number in the sequence is %ld", n, fib[n-1]);
    free(fib);

    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