C LAB‎ > ‎(Sem1) Introduction to C‎ > ‎

WAP to find the sum of Fibonacci series

posted Nov 6, 2010, 1:50 AM by Neil Mathew   [ updated Nov 6, 2010, 1:57 AM ]

SOURCE CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<stdio.h>
 
main()
{
 
int sum=0; 
int n;
 
printf("\n Till which term should the Fibonacci series be found: ");
scanf(" %d ", &n);
 
printf("\n The Fibonacci Series: \n ");
 
if( n==0 )
{
printf(" None ");
}
else if( n==1 )
{
printf("0");
}
else
{
int i,temp;
int first=0,second=1;
 
//2nd term
printf("%d + %d ",first,second);
sum+=second;
 
for(i=3; i<=n; i++) //3rd term onwards
{
temp=second;
second=second+first;
first=temp;
 
printf("+ %d ",second);
sum+=second;
}
}
printf("\n Sum = %d",sum);
 
}


OUTPUT:

 Till which term should the Fibonacci series be found: 0

 The Fibonacci Series: 
  None 
 Sum = 0

 Till which term should the Fibonacci series be found: 1

 The Fibonacci Series: 
 0
 Sum = 0

 Till which term should the Fibonacci series be found: 2

 The Fibonacci Series: 
 0 + 1 
 Sum = 1

 Till which term should the Fibonacci series be found: 8

 The Fibonacci Series: 
 0 + 1 + 1 + 2 + 3 + 5 + 8 + 13 
 Sum = 33



Comments