Home > Net >  How to print fibonacci series upto 50 terms in c?
How to print fibonacci series upto 50 terms in c?

Time:06-09

I want to print the Fibonacci series up to 50 terms but when the n is 50 it's not working it works until n <= 48. Is there any way to do that?

#include <stdio.h>

int main()
{
    int n;
    scanf("%d", &n);
    unsigned long long arr[50] = {0, 1};

    for (int i = 0, j = 2; i < n; i  , j  )
    {
        arr[j] = arr[i]   arr[i   1];
    }

    for (int i = 0; i < n; i  )
    {
        printf("%d => %llu\n", i, arr[i]);
    }

    return 0;
}

CodePudding user response:

#include <stdio.h>
int main() {

  int i, n;

  // initialize first and second terms
  int t1 = 0, t2 = 1;

  // initialize the next term (3rd term)
  int nextTerm = t1   t2;

  // get no. of terms from user
  printf("Enter the number of terms: ");
  scanf("%d", &n);

  // print the first two terms t1 and t2
  printf("Fibonacci Series: %d, %d, ", t1, t2);

  // print 3rd to nth terms
  for (i = 3; i <= n;   i) {
    printf("%d, ", nextTerm);
    t1 = t2;
    t2 = nextTerm;
    nextTerm = t1   t2;
  }

  return 0;
}

CodePudding user response:

  void printFibonacci(int n)
  {
    static int n1=0,n2=1,n3; 
    if(n>0)
    { 
     n3 = n1   n2;    
     n1 = n2;    
     n2 = n3;    
     printf("%d ",n3);    
     printFibonacci(n-1); 
    }   
}
  • Related