Using recursion, display the first 10 terms of Fibonacci Series.
Language: Python, Java, JavaScript accepted.
Urgent.
Share
Using recursion, display the first 10 terms of Fibonacci Series.
Language: Python, Java, JavaScript accepted.
Urgent.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
PYTHON PROGRAM:
def recur_fibo(n):
if n <= 1:
return n
else:
return(recur_fibo(n-1) + recur_fibo(n-2))
nterms = 10
# check if the number of terms is valid
if nterms <= 0:
print("Plese enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(nterms):
print(recur_fibo(i))
Verified answer
[tex]\textsf{\large \textbf{Program}}[/tex]
[tex]\texttt{public class FibonacciUsingRecursion \{}\\\\\texttt{\hspace{2em} static int fibonacci(int n) \{}\\ \texttt{\hspace{4em} if (n < 2) return n;}\\\texttt{\hspace{4em} return fibonacci(n - 1) + fibonacci(n - 2);}\\\texttt{\hspace{2em} \}}\\[/tex]
[tex]\texttt{\hspace{2em} public static void main(String[] args) \{}\\\texttt{\hspace{4em} System.out.print("Fibonacci series (10 terms) - ");}\\\texttt{\hspace{4em} for (int i = 1; i < 11; i++)}\\\texttt{\hspace{6em} System.out.print(fibonacci(i) + " ");}\\\texttt{\}\}}[/tex]
[tex]\textsf{\large \textbf{Outpu\symbol{116} }}[/tex]
[tex]\texttt{Fibonacci series (10 terms) - 1 1 2 3 5 8 13 21 34 55}[/tex]