Enter your email address:


Monday, September 2, 2013

Home » , » Write a program to generate the Fibonacci series.

Write a program to generate the Fibonacci series.

Questions: Top-30-c-programs-asked-in-interview_6304.html
11. Write a program to generate the Fibonacci series.
Fibonacci series: Any number in the series is obtained by adding the previous two numbers of the series.
Let f(n) be n'th term.
f(0)=0;
f(1)=1;
f(n)=f(n-1 )+f(n-2); (for n>=2)
Series is as follows
011
(1+0)
2 (1+1)
3 (1+2)
5 (2+3)
8 (3+5)
13 (5+8)
21 (8+13)
34 (13+21)
...and so on
Program: to generate Fibonacci Series(10 terms)
#include
int main(){
//array fib stores numbers of fibonacci series
int i, fib[25];
// initialized first element to 0
fib[0] = 0;
// initialized second element to 1
fib[1] = 1;
//loop to generate ten elements
for (i = 2; i<10; i++){
//i'th element of series is equal to the sum of i-1'th element and i-2'th element.
fib[i] = fib[i - 1] + fib[i - 2];
}
printf("Th e fibonacci series is as follows \n");
//print all numbers in the series
for (i = 0; i<10; i++){
printf("%d \n", fib[i]);
}
return 0;
}
Output:
The fibonacci series is as follows
01123581
3
21
34
Explanatio n:
The first two elements are initialize d to 0, 1 respective ly. Other elements in the series are generated by looping
and adding previous two numbes. These numbers are stored in an array and ten elements of the series are
printed as output.
Share this games :

0 comments:

Post a Comment

Related Posts