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).
#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;
}
Comments
Leave a comment