C program for fibonacci series


#include<stdio.h>
#include<conio.h>
void main()
{
 int n;
 long int i;
 long int fibo(int n);
 clrscr();
 printf("Enter the limit:\n");
 scanf("%d",&n);
   i=fibo(n);
 printf("\nThe %dth Fibonacci number is %ld",n,i);
 getch();
}

long int fibo(int n)
{
 int old_no,currnt_no,sum,i;
   i=1;
   old_no=0;
   currnt_no=1;
   while(i<=n)
   {
      sum=old_no+currnt_no;
      old_no=currnt_no;
      currnt_no=sum;
      i++;
      printf("\n%d",sum);
   }
 return(sum);
}

Output:-

Enter the limit:

5

1
2
3
5
8
The 5th Fibonacci number is 8

0 comments :