Fibonacci series is a series where f(n-1), f(n),f(n+1) are three terms in fibonacci series, then
f(n+1)=f(n-1)+f(n)
Each terms in fibonacci series is sum of previous two terms taking f(1)=1 and f(2)=1
The Series generated is as follow
1, 1, 2, 3, 5, 8, 13, 21, 34, 55 ...............
Suppose we want to print fibonacci series upto 'n' numbers.
We know two intial terms of the series which are f(1)=1 and f(2)=1. Print these on the screen. take count =0 which will make the program loop till it is equal to 'n-2'. [first two terms are printed outside loop. So number of terms entered is subtracted by 2. n=n-2]
a=1, b=1 ,c=0,count=0
print(a)
print(b)
repeat until count is less than 'n-2'
1. c = a + b
2. print (c)
3. a = b
4. b = c
5. count = count +1
for example n=5
a=1,b=1
n=n-2
count=0
print(a), print(b)
count<3 [0<3 yes]
c = a + b [a = 1, b =1,c = 1 + 1 = 2]
print (c) [ prints 2]
a = b [a=1]
b = c [b=2]
count = count+1 [count = 0 + 1 =1]
count<3 [1<3 yes]
c = a + b [a = 1, b =2,c = 1 + 2 = 3]
print (c) [ prints 3]
a = b [a=2]
b = c [b=3]
count = count+1 [count = 1 + 1 = 2]
count<3 [2<3 yes]
c = a + b [a = 2, b =3,c = 2 + 3 = 5]
print (c) [ prints 5]
a = b [a=3]
b = c [b=5]
count = count+1 [count = 2 + 1 =3]
count<3 [ 3<3 No. Loop stops]
for n= 5, series containing 5 terms is printed
1 1 2 3 5
#include<stdio.h>
int main()
{
int a, b, c, n, count;
printf("\nFibonacci Series");
printf("\nEnter number of terms(number>2):");
scanf("%d",&n);
a=1;
b=1;
printf("%d %d ",a,b);
count=0;
n=n-2;
while(count<n)
{
c=a+b;
printf("%d ",c);
a=b;
b=c;
count++;
}
return 0;
}
f(n+1)=f(n-1)+f(n)
Each terms in fibonacci series is sum of previous two terms taking f(1)=1 and f(2)=1
The Series generated is as follow
1, 1, 2, 3, 5, 8, 13, 21, 34, 55 ...............
Suppose we want to print fibonacci series upto 'n' numbers.
We know two intial terms of the series which are f(1)=1 and f(2)=1. Print these on the screen. take count =0 which will make the program loop till it is equal to 'n-2'. [first two terms are printed outside loop. So number of terms entered is subtracted by 2. n=n-2]
a=1, b=1 ,c=0,count=0
print(a)
print(b)
repeat until count is less than 'n-2'
1. c = a + b
2. print (c)
3. a = b
4. b = c
5. count = count +1
for example n=5
a=1,b=1
n=n-2
count=0
print(a), print(b)
count<3 [0<3 yes]
c = a + b [a = 1, b =1,c = 1 + 1 = 2]
print (c) [ prints 2]
a = b [a=1]
b = c [b=2]
count = count+1 [count = 0 + 1 =1]
count<3 [1<3 yes]
c = a + b [a = 1, b =2,c = 1 + 2 = 3]
print (c) [ prints 3]
a = b [a=2]
b = c [b=3]
count = count+1 [count = 1 + 1 = 2]
count<3 [2<3 yes]
c = a + b [a = 2, b =3,c = 2 + 3 = 5]
print (c) [ prints 5]
a = b [a=3]
b = c [b=5]
count = count+1 [count = 2 + 1 =3]
count<3 [ 3<3 No. Loop stops]
for n= 5, series containing 5 terms is printed
1 1 2 3 5
#include<stdio.h>
int main()
{
int a, b, c, n, count;
printf("\nFibonacci Series");
printf("\nEnter number of terms(number>2):");
scanf("%d",&n);
a=1;
b=1;
printf("%d %d ",a,b);
count=0;
n=n-2;
while(count<n)
{
c=a+b;
printf("%d ",c);
a=b;
b=c;
count++;
}
return 0;
}
No comments:
Post a Comment