-4

我想用 C 生成斐波那契数列。我的代码给出了编译错误。这是代码,实际上我是编程的初学者。

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

8 回答 8

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;
}
于 2013-06-21T07:26:55.893 回答
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);
    }
}  
于 2013-06-21T07:35:04.937 回答
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);
            } 
        }
    }
}
于 2013-06-21T12:04:50.083 回答
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;
}

上述程序的输出

于 2021-06-27T12:59:40.043 回答
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;
  }
}
于 2016-05-13T12:51:19.463 回答
0

Java 代码

公共类新类{

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

}

于 2019-09-02T18:55:59.083 回答
0
#include <stdio.h>

int main(){
    int intNumber;
    int intPostNumber=1;
    int intPreNumber=1;
    printf("Enter number :");
    scanf("%d",&intNumber);

    printf("%d, %d, ",intPostNumber,intPreNumber);
    for(int i=2;i<intNumber;i++){
        int intTemp=intPreNumber;
        intPreNumber+=intPostNumber;
        intPostNumber=intTemp;
        printf("%d, ",intPreNumber);
    }
    printf("\b\b ");
    return 0;
}
于 2021-08-20T10:11:13.543 回答
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);

 }

 }
于 2016-01-23T04:37:15.027 回答