Question & Answer: Write a C program to add the first 50 items of Fibonacci Sequence 1, 1, 2, 3, 5, 8, 13, 21………………50th item……

Write a C program to add the first 50 items of Fibonacci Sequence 1, 1, 2, 3, 5, 8, 13, 21………………50th item.

Expert Answer

 

Don't use plagiarized sources. Get Your Custom Essay on
Question & Answer: Write a C program to add the first 50 items of Fibonacci Sequence 1, 1, 2, 3, 5, 8, 13, 21………………50th item……
GET AN ESSAY WRITTEN FOR YOU FROM AS LOW AS $13/PAGE
Order Essay

C program :

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

int main()
{
long long fib[51];
//Initializing first and second fibonacci number with 1 and 1
fib[1] = 1;
fib[2] = 1;
int n = 50;
//Initially sum of the first and second fibonacci numbers is 1 + 1 = 2;
long long ans = 2;
for(int i=3;i<=n;i++){
//Calculate ith fibonacci number
fib[i] = fib[i-1] + fib[i-2];
//Calculate the answer
ans = ans + fib[i];
}
printf(“Sum of the first 50 fibonacci terms is %lldn”,ans);

return 0;
}

OUTPUT :

Question & Answer: Write a C program to add the first 50 items of Fibonacci Sequence 1, 1, 2, 3, 5, 8, 13, 21..................50th item...... 1

Still stressed from student homework?
Get quality assistance from academic writers!