如何在C中生成Fibonacci系列

时间:2013-06-21 07:18:45

标签: c fibonacci

我想在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);
   }
}

7 个答案:

答案 0 :(得分:3)

这很有效。

#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;
}

答案 1 :(得分:1)

此代码将打印前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);
    }
}  

答案 2 :(得分:0)

这适用于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);
            } 
        }
    }
}

答案 3 :(得分:0)

#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);

 }

 }

答案 4 :(得分:0)

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;
  }
}

答案 5 :(得分:0)

Java代码

公共类NewClass {

public void Fibonacci_Series(int input) 
{
    int a=0;
    int b=1;
    for(int i=1;i<=input;i++)
    {
           if(input==1){
                System.out.println(a);
                break; }

           else if(input==2){
                System.out.println(+a);
                System.out.println(+b);
                break;      }

           else if (input>2){
           int c,sum=a+b;
           System.out.println(sum);
           a=b;
           b=sum;           } 
    }
}

}

答案 6 :(得分:0)

我可以向你保证,这段代码在生成最多 n 项的斐波那契数列时运行得非常好

#include<stdio.h>
#include<conio.h>
int main()
{
   int n,i,f=0,s=1,number;
   printf("\nEnter the number of terms:");
   scanf("%d",&n);
   printf("\nThe fibonacci series upto term %d is:",n);
   printf("\n%d\n%d",f,s);
   for(i=3;i<=n;i++)
   {
        number=f+s;
        f=s;
        s=number;
        printf("\n%d ",number);
   }
   return 0;
}

Output for the above Program