def fib (n):
if n == 0 or n ==1:
return n
else:
return fib(n-2) + fib(n-1)
#n = int(input("please enter a number "))
n=10
for index,num in enumerate(range(n+1)):
print(f"F{index} :", fib(num))
def fibo2(n):
if n ==1 or n==0:
return 1
else :
n = n+fibo2(n-1)
return n
print(fibo2(n1))
function fib(n) {
if (n === 1 || n === 0)
return n
return fib(n - 1) + fib(n - 2)
}
console.log('fib:', fib(9)) // [Log]: fib: 34
// Time complexity O(2^n)
// Space complexity O(n)
// Since fib(n-1) gets evaluated completely before fib(n-2), there will never be more than n levels of recursion.
#include<stdio.h>
int main()
{
int first=0, second=1, i, n, sum=0;
printf("Enter the number of terms: ");
scanf("%d",&n);
//accepting the terms
printf("Fibonacci Series:");
for(i=0 ; i<n ; i++)
{
if(i <= 1)
{
sum=i;
}
//to print 0 and 1
else
{
sum=first + second;
first=second;
second=sum;
//to calculate the remaining terms.
//value of first and second changes as new term is printed.
}
printf(" %d",sum)
}
return 0;
}
int fib (int n) {
if (n < 2)
return 1;
return fib(n-1) + fib(n-2);
}
def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n - 1) + fibonacci(n - 2)
n = int(input())
result = fibonacci(n)
print(result)