Answer to Question #328279 in C for Mominul

Question #328279

Suppose you are given a sequence of numbers as follows 1, 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-14T11:24:18-0400
#include <stdio.h>


int main() {
    int seq2=1, seq1=1, seq;    // 
    int n;

    printf("Enter n: ");
    scanf("%d", &n);

    if (n <= 0) {
        printf("n should be positive");
        return 1;
    }
    if (n <= 2) {
        seq = 1;
    }

    for (int i=2; i<n; i++) {
        seq = seq1 + seq2;
        seq2 = seq1;
        seq1 = seq;
    }

    printf("The %d number of the sequence is %d", n, seq);

    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