Loading…
DAA · Dynamic programming · Program 11 · In the record
Write a program in c to find the fibonacci series using dynamic programming and find its execution time using the time function.
Algorithm: FibonacciDP(n) 1. Start 2. For i <- 0 to (n-1) do 3. if (i <= 1) then 4. F[i] <- i 5. else 6. F[i] <- F[i-1] + F[i-2] 7. Print F[0..n-1] 8. Stop
1#include<stdio.h>2#include<time.h>3 4int main() {5 int n,i,count=0;6 printf("Enter the number of terms:");7 scanf("%d",&n);8 int f[n];9 10 clock_t start=clock();11 for(i=0;i<n;i++) {12 count++;13 if(i<=1)14 f[i]=i;15 else16 f[i]=f[i-1]+f[i-2];17 }18 clock_t end=clock();19 double time_taken=(double)(end-start)/CLOCKS_PER_SEC;20 21 printf("\nFibonacci series:");22 for(i=0;i<n;i++)23 printf("\t%d",f[i]);24 printf("\nNumber of iterations:%d",count);25 printf("\nTime taken:%f seconds",time_taken);26 printf("\nTime complexity: O(n)");27 printf("\nSpace complexity: O(n)");28 printf("\n");29 return 0;30}10Enter the number of terms:10 Fibonacci series: 0 1 1 2 3 5 8 13 21 34 Number of iterations:10 Time taken:0.000003 seconds Time complexity: O(n) Space complexity: O(n)
Run program 11 in your browser
C · clang → WebAssembly. Change the code or the input, run it, break it, and see what happens.