我想在C中生成Fibonacci系列。我的代码给出了编译错误。这是代码,实际上我是编程的初学者。
main()
{
int n, first = 0, second = 1, next, c;
printf("Enter the number of terms\n");
scanf("%d",&n);
printf("First %d terms of Fibonacci series are :-\n",n);
for ( c = 0 ; c < n ; c++ )
{
if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
}
printf("%d\n",next);
}
}
这很好用。
#include<stdio.h>
int main()
{
int n, first = 0, second = 1, next, c;
printf("Enter the number of terms\n");
scanf("%d",&n);
printf("First %d terms of Fibonacci series are :-\n",n);
for ( c = 0 ; c < n ; c++ )
{
if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
}
printf("%d\n",next);
}
return 0;
}
此代码将打印前5个斐波那契数字
#include<stdio.h>
void main()
{
int first,second,next,i,n;
first=0;
second=1;
n=5;
printf("\n%d\n%d",first,second);
for(i=0;i<n;i++)
{
next=first+second;//sum of numbers
first=second;
second=next;
printf("\n%d",next);
}
}
#include<stdio.h>// header files
#include<conio.h>
void main()
{
int f1=0,f2=1,f,n,i;
printf("enter the no of terms");
scanf("%d",&n);
printf("the fibbonacci series:\n");
printf("%d\n%d",f1,f2);
for(i=2;i<n;i++)
{
f=f1+f2;
f1=f2;
f2=f;
printf("%d\n",f);
}
}
这将适用于n的所有值。
void fibSeries(int n)
{
int first = 0, next = 1, index= 0;
if(n <= 0)
return;
if(n == 1)
printf("%d ", first);
else
{
printf("%d %d ", first, next);
if(n > 2)
{
while(index++ < (n-2))
{
int temp = first + next;
first = next;
next = temp;
printf("%d ", next);
}
}
}
}
void main()
{
int a,b,c;
a = 0;
b = 1 ;
c = a + b;
printf(" %d ",a);
printf(" %d ",b);
while ( c <= 100)
{
printf(" %d ",c);
a = b;
b = c;
c = a + b;
}
}