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

 

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 :

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