ccplusplus.com
Learn C, C++ Concepts
Friday, October 7, 2011
fibonacci series in c using recursion
/************************************************************* * File : fibonacci-series-using-recursion.c * Author : Saurabh Gupta * Desc : fibonacci series using recursion * fibonacci series using recursion in c * c program to find fibonacci series * Source : http://saurabhgupta0527.blogspot.com/p/c.html * Created : AM 09:25 10 October 2011 ************************************************************/ #include<stdio.h> int fibonacci(int); int main() { int nFibNumber, nCounter = 0, nTemp; printf ("Enter a number to print the fibonacci series : "); scanf("%d", &nFibNumber); printf("Fibonacci series ->\n"); for ( nTemp = 1 ; nTemp <= nFibNumber ; nTemp++ ) { printf("%d\t", fibonacci(nCounter)); nCounter++; } printf ("\n"); return 0; } int fibonacci(int nFibNumber) { if ( nFibNumber == 0 ) { return 0; } else if ( nFibNumber == 1 ) { return 1; } else { return ( fibonacci(nFibNumber-1) + fibonacci(nFibNumber-2) ); } } /* * OUTPUT * [sgupta@rhel54x64 c]$ gcc fibonacci-series-using-recursion.c -o fibonacci-series-using-recursion [sgupta@rhel54x64 c]$ ./fibonacci-series-using-recursion Enter a number to print the fibonacci series : 10 Fibonacci series -> 0 1 1 2 3 5 8 13 21 34 [sgupta@rhel54x64 c]$ */
See also
Other popular tricky C Sample Codes and language Concept
.
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment