C language implementation of Fibonacci sequence of non recursive examples

  • 2020-05-27 06:33:56
  • OfStack

Without further ado, go straight to the code


#include <stdio.h>
#include <stdlib.h>

void f(int n);

int main(void)
{
 f(10);
 return 0;
}

void f(int n)
{
 if(n==1)
 {
  printf("1\n");
  return;
 }
 if(n==2)
 {
  printf("1 1\n");
  return;
 }
 printf("1 1 ");
 int* p=(int*)malloc(sizeof(int)*n);
 p[0]=1;
 p[1]=1;
 int i;
 for(i=2; i<n; i++)
 {
  p[i]=p[i-1]+p[i-2];
  printf("%d ", p[i]);
 }
 printf("\n");
 free(p);
 p=NULL;
}

Related articles: