C Online Compiler
Example: Fibonacci Series up to 100 in C using Recursion
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Fibonacci Series up to 100 in C using Recursion #include <stdio.h> // Recursive Function to generate the Fibonacci series int rec_fibonacci(int num, int term_1, int term_2, int next_term) { printf("%d, ", next_term); term_1 = term_2; term_2 = next_term; next_term = term_1 + term_2; if (next_term <= num) rec_fibonacci(num, term_1, term_2, next_term); else return 0; } // It's the main function of the program int main() { int num; int term_1 = 0, term_2 = 1, next_term = 0; // Step-1 Take input from the User printf("INPUT POSITIVE NUMBER: "); scanf("%d", &num); // Step-2 Print the first two terms which are always 0 and 1 printf("\nFIBONACCI SERIES: %d, %d, ", term_1, term_2); next_term = term_1 + term_2; // Step-3 Call the recursive function:rec_fibonacci() to generate the fibonacci series rec_fibonacci(num, term_1, term_2, next_term); return 0; }
500
Output
Clear
ADVERTISEMENTS