Recursion is the repeated application of a recursive procedure or definition. A recursive function can simulate a loop by call itself multiple times until a base condition is satisfied. A recursive function without an exit or base condition will fall into an infinite loop.
void recursive_func()
{
//body
recursive_func();
}
The main() function can be made into a recursive function by calling it from within the body.
void main()
{
//body
main();
}
Fibonacci series Fn is defined as a series of numbers in which each number ( Fibonacci number ) is the sum of the two preceding numbers.
F0 = 0, F1 = 1, and
Fn = Fn-1 + Fn-2 , for n > 1.
therefore, Fibonacci Series := 0, 1, 1, 2, 3, 5, 8, 13, ...
#include <stdio.h>
int fibonacci(int);
int main(void)
{
int no_of_terms,i;
printf("Enter the number of terms: ");
scanf("%d", &no_of_terms);
printf("Fibonacci Series: ");
for ( i = 0 ; i < no_of_terms ; i++ )
{
//function call
printf("%d ", fibonacci(i));
}
return 0;
}
int fibonacci(int n)
{
//base condition
if ( n == 0 || n == 1)
return n;
//recursive call
else
return ( fibonacci(n-1) + fibonacci(n-2) );
}
Enter the number of terms: 10
Fibonacci Series: 0 1 1 2 3 5 8 13 21 34
C Programming
Overview Hello World Installation Comments Header Files Data Types Tokens Operators Input/Output Format Specifiers Decision Control Loops Type Qualifiers Storage class specifier Arrays Enums Structures Unions Functions Recursions Inline Functions Pointers Pointer Arithematic Call by value and call by reference Linked Lists Passing of Arrays in Functions Preprocessor and Macros Mathematical Operations String Operations Bit Fields Command Line Arguments Variable Arguments Error Handling Type Casting Typedef File Handling Multi Processing Threads Outdated C Programming Memory Management Compilation C Programs Interview Questions MiscellaneousTrending
C Programming
Remember to follow community guidelines and don't go off topic.