Skip to content

Instantly share code, notes, and snippets.

@tiptopcoder
Created May 15, 2016 05:50
Show Gist options
  • Save tiptopcoder/d03bbaa6f3999f4c4cc0978f81f53937 to your computer and use it in GitHub Desktop.
Save tiptopcoder/d03bbaa6f3999f4c4cc0978f81f53937 to your computer and use it in GitHub Desktop.
Fibonacci using Dynamic Array
#include <stdio.h>
#include <stdlib.h>
int main ()
{
double *fib;
int n, i;
printf("How many periods would you like? ");
scanf("%d", &n );
fib = (double *) malloc ( n * sizeof(double) );
if ( fib == NULL )
{
printf("Error\n");
exit (-1);
}
for ( i = 0 ; i <= n ; i++ )
{
if ( i == 0 || i == 1 )
{
fib[i] = i;
}
else
{
fib[i] = fib[i-1] + fib[i-2];
}
printf("fibonacci[%02d] = %7.0f ", i, *(fib + i) );
//Break line after 4 elements per row
if ( i % 3 == 2 )
{
printf("\n");
}
}
printf("\n");
printf("%.1lf", *(fib+n));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment