int fib_recursive(int n){ if(n==1 || n==2){ return n-1; } else{ return fib_recursive(n-1)+fib_recursive(n-2); } } int fib_iterative(int n){ int a=0; int b=1; for(int i=0; i<n-1; i++){ b=a+b; a=b-a; } return a; } int main(){ int num; printf("enter the index to get fibonacci series\n"); scanf("%d",&num); printf("value of fibonacci number at position %d using interative approach is %d\n",num,fib_iterative(num)); printf("value of fibonacci number at position %d using recursive approach is %d\n",num,fib_recursive(num)); }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter