-5

I want to generate Fibonacci series in C. My code is giving a compilation error. Here is the code, Actually I am a beginner in programming.

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);
   }
}
Sujeet Kumar
  • 1,280
  • 1
  • 17
  • 25
  • 9
    Please tell us what you mean by "not working". Does the program crash? If so where? Does it produce an incorrect result? – Eric J. Jun 21 '13 at 07:19
  • 2
    Actually It give a compilation error. – Sujeet Kumar Jun 21 '13 at 07:22
  • ...and the errors are...? – Jeff Mercado Jun 21 '13 at 07:23
  • @NeeraJones because the compiler wants `int main()` instead of only `main()`? – stefan Jun 21 '13 at 07:23
  • Was this the error: _'printf' was not declared in this scope_ – legends2k Jun 21 '13 at 07:23
  • @stefan: That usually gives only a warning, no? – legends2k Jun 21 '13 at 07:24
  • @NeeraJones Don't forget to put `#include ` at the top for your `printf` in addition to the `main` problem. Also right before `main` concludes you should return something like `return 0;` – Nobilis Jun 21 '13 at 07:24
  • @legends2k it's not standard conforming to have a function return nothing at all, it should produce an error. – stefan Jun 21 '13 at 07:24
  • @stefan: I have to say I'm not sure for C, but in C++ it is legal (although bad style) to have no `return` statement in `main()` even though it is declared as `int`. Could it be that the two languages share this particularity ? – ereOn Jun 21 '13 at 07:27
  • @stefan: Of course it isn't and I knew it, but when you invoke the compiler normally i.e. `gcc file.c` or `cl /EHsc file.c` would compile this just fine, since warnings aren't ON by default. – legends2k Jun 21 '13 at 07:27
  • @ereOn it's not about the missing return statement (that's fine in `C++` at least, don't know about `C`), it's about the missing return _type_ – stefan Jun 21 '13 at 07:28
  • Thanks all, Now Its working. I used int main() and a return statement. @Nobilis – Sujeet Kumar Jun 21 '13 at 07:28
  • best way to generate fibonacci number quickly would be Matrix Exponentation. you can even generate for bigger number Modulo Prime number in O(log n). – S J Jun 21 '13 at 08:29
  • Wow. The answers below are a mess. I'm protecting this so that fewer low-rep users can add more bad answers, but I hope that others will join me in closing this question. – ad absurdum Sep 17 '22 at 18:51

9 Answers9

3

This works well.

#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;
}
PythonIITD
  • 85
  • 8
1

This code will print the first 5 fibonnacci numbers

#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);
    }
}  
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
Chetan Gawai
  • 2,361
  • 1
  • 25
  • 36
0

This will work for all the values of 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);
            } 
        }
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Rupali K
  • 66
  • 2
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);

 }

 }
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;
  }
}
Ghanshyam Anand
  • 164
  • 1
  • 10
0

Code for Java

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

}

0

I can assure you that this code will run perfectly fine for generating fibonacci series upto n terms

#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

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;
}
0
#include<stdio.h>
void main(){
    int i=1,in,f=1,s=0,n;
    printf("Enter value: ");
    scanf("%d",&in);
    while(i<=in){
        n = f+s;
        s = f;
        f = n;
        printf(" %d",n);
        i=s+f;
    }
}

This code returns the Fibonacci series with an input limit.

If you entered 30 as an input.

Output: 1 2 3 5 8 13 21

M DEV
  • 763
  • 1
  • 7
  • 20