From 55c41ca9c39708584c5272a70e04e1d9e2483ee0 Mon Sep 17 00:00:00 2001 From: Megha Sharma Date: Tue, 20 Oct 2020 00:58:57 +0530 Subject: [PATCH] 'Fibonacci.c' --- CalculatorFunctions/fibonacci.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/CalculatorFunctions/fibonacci.c b/CalculatorFunctions/fibonacci.c index 30d03df..2dafcd7 100644 --- a/CalculatorFunctions/fibonacci.c +++ b/CalculatorFunctions/fibonacci.c @@ -1 +1,29 @@ /* Make a function that calculates the first n fibonacci numbers. Bonus points if it is recursive! */ +#include +int fibonacci(int n) +{ //recursive function for generating fibonacci series + if(n == 0) + { //base case + return 0; + } + else if(n == 1) + { //for second term + return 1; + } + else + { + return (fibonacci(n-1) + fibonacci(n-2)); //uses previous values to generate the next value + } +} + +int main() +{ //main function to run the code + int n,i; + printf("Enter the number of terms: "); + scanf("%d",&n); //taking number of terms as user input + printf("Fibonacci of %d: " , n); + for(i=0;i