Cevap :
// Program for Fibonacci Number
#include <stdio.h>
void main()
{
int f1 = 0, f2 = 1, f3, n;
printf("Program for Fibonacci Series\n");
printf("Enter the maximum number for Fibonacci Series: ");
scanf("%d", &n);
printf("\nPrinting Fibonacci Series from 0 - %d\n", n);
printf("%d\n%d\n", f1, f2);
while(1)
{
f3 = f1 + f2;
if(f3 > n)
break;
printf("%d\n", f3);
f1 = f2;
f2 = f3;
}
}
Output
Program for Fibonacci Series
Enter the maximum number for Fibonacci Series:
Printing Fibonacci Series from 0 - 1000
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
---------------------------------------------------
#include<stdio.h> int main(){ int k,r; longint i=0l,j=1,f; //Taking maximum numbers form user printf("Enter the number range:"); scanf("%d",&r); printf("FIBONACCI SERIES: "); printf("%ld %ld",i,j);//printing firts two values. for(k=2;k<r;k++){ f=i+j; i=j; j=f; printf(" %ld",j); } return0; }
--------------------------------------
Algorithm: ( Fibonacci number generator) Iterative . Step 01: function fib(n : integer) return integer is Step 01: first : integer := 0; Step 01: second : integer := 1; Step 01: tmp : integer; Step 01: begin Step 01: for i in 1..n loop Step 01: tmp := first + second; Step 01: first := second; Step 01: second := tmp; Step 01: end loop; Step 01: return first; Step 01: end fib;Source code: (Fibonacci number generator) # include<stdio.h>
# include<conio.h>
void fibonacci(long);
int main()
{
clrscr();
long N;
scanf("%ld",&N);
fibonacci(N);
getch();
return 0;
}
void fibonacci(long N)
{
long i,f0=0,f1=1,f;
for(i=0;i<N;i++)
{
f=f0+f1;
f1=f0;
f0=f;
printf("%3d",f1);
}
}
OR # include<stdio.h>
# include<conio.h>
int main()
{
clrscr();
long N, i,f0=0,f1=1,f;
scanf("%ld",&N);
for(i=0;i<N;i++)
{
f=f0+f1;
f1=f0;
f0=f;
printf("%3d",f1);
}
getch();
return 0;
}
Sample Input Sample output
5 0 1 1 2 3 8 0 1 1 2 3 5 8 13 10 0 1 1 2 3 5 8 13 21 34 3 0 1 1