Calculate sum of Fibonacci series - C Program

Write a C program to calculate sum of Fibonacci series up to given limit.

Solution:

A series in which each number is sum of its previous two numbers is known as Fibonacci series. Each number in series is called as Fibonacci number.
In this program, we assume that first two Fibonacci numbers are 0 and 1.

#include <stdio.h>
#include <math.h>
int main()
{
      int f1,f2,f3,n,i=2,s=1;
      f1=0;
      f2=1;
      printf("How many terms do you \nwant in Fibonacci series? : ");
      scanf("%d",&n);
      printf("\nFibonacci Series Upto %d Terms:\n\n",n);
      printf("%d, %d",f1,f2);
      while(i<n)
      {
            f3=f1+f2;
            printf(", %d",f3);
            f1=f2;
            f2=f3;
            s=s+f3;
            i++;
      }
      printf("\n\nSum of Fibonacci Series : %d",s);
      return 0;
}


Output:

sum fibonacci series

C Program to produce a Fibonacci series that starts and ends with entered terms. Print the sum of series.

Solution:

#include <stdio.h>
#include <math.h>
int main()
{
      int f1,f2,f3,n,i=2;
      printf("How many terms do you \nwant in Fibonacci Series? : ");
      scanf("%d",&n);
      printf("\nEnter first term of series  : ");
      scanf("%d",&f1);
      printf("\nEnter second term of series : ");
      scanf("%d",&f2);
      printf("\nFibonacci Series :\n\n");
      printf("%d, %d",f1,f2);
      int s=f1+f2;
      while(i<n)
      {
            f3=f1+f2;
            printf(", %d",f3);
            f1=f2;
            f2=f3;
            s=s+f3;
            i++;
      }
      printf("\n\nSum of above Fibonacci series : %d",s);
      return 0;
}


Output:

sum fibonacci series