Recursive Fibonacci (Id-1249)
Given the value of 'n', write a recursive routine in C to print the first 'n' elements of the Fibonacci series. Fibonacci series is obtained by the sum of the preceding two terms in the series. The first two terms are 0 and 1.
Eg.,
for n=7,
the Fibonacci terms are 0,1,1,2,3,5,8
Input Format:
Value of 'n'
Output Format:
Fibonacci series of 'n' terms, each term separated by a space
SOLUTION:
C-Code:
#include<stdio.h>
void main()
{
int n,a,b,c,i;
scanf("%d",&n);
printf("0 1 ");
a=0;
b=1;
for(i=0;i<n-2;i++)
{
c=a+b;
a=b;
b=c;
printf("%d ",c);
}
}
INPUT:
Enter the value if n
PROCESSING:
int n,a,b,c,i;
scanf("%d",&n);
printf("0 1 ");
a=0;
b=1;
for(i=0;i<n-2;i++)
{
c=a+b;
a=b;
b=c;
printf("%d ",c);
}
OUTPUT:
Fibonacci series of 'n' terms
Pseudo Code:
1)START
2)Read the value of n
3)Print 0,1 initially
4)Assume a is equal to zero and b is equals to one
5)For i in the range of n minus two get the c value which is equal to sum of a and b
6)Now consider a as b and b as c and continue to for loop in the step-5
7)Display the value of c
8)END
Given the value of 'n', write a recursive routine in C to print the first 'n' elements of the Fibonacci series. Fibonacci series is obtained by the sum of the preceding two terms in the series. The first two terms are 0 and 1.
Eg.,
for n=7,
the Fibonacci terms are 0,1,1,2,3,5,8
Input Format:
Value of 'n'
Output Format:
Fibonacci series of 'n' terms, each term separated by a space
SOLUTION:
C-Code:
#include<stdio.h>
void main()
{
int n,a,b,c,i;
scanf("%d",&n);
printf("0 1 ");
a=0;
b=1;
for(i=0;i<n-2;i++)
{
c=a+b;
a=b;
b=c;
printf("%d ",c);
}
}
INPUT:
Enter the value if n
PROCESSING:
int n,a,b,c,i;
scanf("%d",&n);
printf("0 1 ");
a=0;
b=1;
for(i=0;i<n-2;i++)
{
c=a+b;
a=b;
b=c;
printf("%d ",c);
}
OUTPUT:
Fibonacci series of 'n' terms
Pseudo Code:
1)START
2)Read the value of n
3)Print 0,1 initially
4)Assume a is equal to zero and b is equals to one
5)For i in the range of n minus two get the c value which is equal to sum of a and b
6)Now consider a as b and b as c and continue to for loop in the step-5
7)Display the value of c
8)END
0 Comments