Open the IDE and type the program.
CODING:
#include<stdio.h>main()
{
int f1=-1, f2=1;
fib(f1,f2);
getch();
}
fib(fib1,fib2)
{
int fib3, n,i;
printf("\nFIBONCCI SERIES USING FUNCTION");
printf("\n\nEnter the no.of terms") ;
scanf("%d", &n);
printf("\n\n The fibonocci series is..\n");
for(i=0;i<n;i++)
{
fib3=fib1+fib2;
fib1=fib2;
fib2=fib3;
printf("\n%d", fib3);
}
}
OUTPUT:
EXPLANATION:
First f1=-1, f2=1 is assigned. Next it is passed to Fib(fib1, fib2). for loop is executed for n times.
Here, for example I gave 5 as n value.
For i=0, fib1=-1, fib2=1 then fib3=fib1+fib2(-1+1=0). Then, fib1=1(ie. fib2).fib2=0(ie.fib3)
For i=1, fib3=1+0=1. Then fib1=0. fib2=1.
For i=2, fib3=0+1=1. Then fib1=1. fib2=1.
For i=3, fib3=1+1=2. Then fib1=1. fib2=2.
For i=4, fib3=2+1=3. Then fib1=2. fib2=3.
The output of fib3 is printed for n times.
Good Explanation
ReplyDelete