Question

Write a recursive static method that will return the n’th term in the Fibonacci series, a...

Write a recursive static method that will return the n’th term in the Fibonacci series, a series of numbers in which each number is the sum of the two preceding numbers. The first terms are 1, 1, 2, 3, 5, 8, 13, … Therefore fibonacci(5) will return 5.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program :

import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
int n,result;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number:");
n=sc.nextInt();
result = fib(n);

System.out.printf("Fibonacci series "+n+"th term is:"+result);
}

public static int fib(int n)
{
if (n ==1 ||n==2)
return 1;
else
return fib(n-1)+fib(n-2);
}
}

Screenshot of the program :

Add a comment
Know the answer?
Add Answer to:
Write a recursive static method that will return the n’th term in the Fibonacci series, a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write an ARM assembly language subroutine (named nfibo) to calculate and return the n-th Fibonacci number....

    Write an ARM assembly language subroutine (named nfibo) to calculate and return the n-th Fibonacci number. Fibonacci numbers (or a Fibonacci sequence) are a series of numbers with a property that the next number in the series is a sum of previous two numbers. Starting the series from 0, 1 as the first two numbers we have 0, 1, (0 + 1) = 1, (1 + 1) = 2, (1 + 2) = 3, (2 + 3) = 5, (3...

  • in C++ 6. (20)The Fibonacci sequence is the series of integers 0, 1, 1,2, 3, 5,...

    in C++ 6. (20)The Fibonacci sequence is the series of integers 0, 1, 1,2, 3, 5, 8, 13, 21, 34, 55, 89.. 1 See the pattern? Each element in the series is the sum of the preceding two items. There is a recursive formula for calculating the nth number of the sequence (the oth number if Fib(0)-0): 8 Fib(N)-/N, if N 0 or 1 ifN> 1 Fib(N-2) Fib(N-1), a. b. c. Write a recursive version of the function Fibonacci. Write...

  • A Fibonacci sequence is a series of numbers where the next number in the series is...

    A Fibonacci sequence is a series of numbers where the next number in the series is the sum of the previous two numbers. The sequence starts with the numbers 0 and 1. Here are the first ten numbers of the Fibonacci sequence:             0 1 1 2 3 5 8 13 21 34 Write a Java program to print out the first fifteen numbers of the Fibonacci sequence using a loop. You will need three variables previous, current, and next...

  • solve questions by using fortoran.90 6. Consider the Fibonacci series: 1 1 2 3 5 8...

    solve questions by using fortoran.90 6. Consider the Fibonacci series: 1 1 2 3 5 8 13 ... Each number in the series (except the first two, which are 1) is the sum from the two previous numbers. Write a program that reads in an integer limit, and which prints the first limit terms of the series. Use an nested if block structure. (You need to distinguish between several cases: limit<0, limit=1, etc.) 7. Rewrite the previous program using a...

  • For the following recursive implementation of a method to compute the Fibonacci S integer n, circle...

    For the following recursive implementation of a method to compute the Fibonacci S integer n, circle the line number(s) the them: s) that comprise the three parts of a recursive algorithm and label 1. public static long fibonacci(int n) ( 2 if( 1) 3. return 1; 4. else if (n 2) S. return; 6. else 7. (long fibNminus1 fibonacci(n - 1); 8. long fibNminus2- fibonacci(n -2); 9. long fibN fibNminusl + fibNminus2; 10. return fibN; 12.)

  • Fibonacci Sequence The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5,...

    Fibonacci Sequence The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... The next number is found by adding up the two numbers before it. The 2 is found by adding the two numbers before it (1+1) The 3 is found by adding the two numbers before it (1+2), And the 5 is (2+3), and so on!         Example: the next number in the sequence above is 21+34 = 55 Source:...

  • CH Question 1. Implement a recursive method fibo that takes an integer num as a parameter...

    CH Question 1. Implement a recursive method fibo that takes an integer num as a parameter and returns the nth Fibonacci number. Fibonacci sequence: 1, 1, 2, 3, 5, 8, 13, ... is an infinite sequence, where first 2 numbers are 1 and every subsequent number is equivalent to the sum of the previous two. Question 2. Implement a recursive method mult that takes two positive integer parameters, x and y and returns their product For example mult(2,3) returns 6....

  • I need help with this code. Im using C++ coding. Non Recursive (iterative) Fibonacci Write a...

    I need help with this code. Im using C++ coding. Non Recursive (iterative) Fibonacci Write a program that uses a for loop to calculate a Fibonacci sequence (NOT RECURSIVE!!!) up to a given position, starting with position 0. This function defines a Fibonacci sequence: If the number is 0, the function returns a 0 If the number is 1, the function returns a 1 If the number is higher than 1, it returns the sum of the previous two numbers...

  • Question 3 Program Language C++ Problem 3 Fibonacci Numbers 10 points Fibonacci numbers are a sequence...

    Question 3 Program Language C++ Problem 3 Fibonacci Numbers 10 points Fibonacci numbers are a sequence of numbers where each number is represented by the sum of the two preceding numbers, starting with 0 and 1: 0, 1, 1, 2, 3, 5, 8, etc Write a program that repeatedly prompts the user for a positive integer and prints out whether or not that integer is a Fibonacci number. The program terminates when-I is entered. Create a method with the following...

  • The Fibonacci sequence is the sequence of numbers: 0, 1, 1, 2, 3, 5, 8, 13,...

    The Fibonacci sequence is the sequence of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, … The next number is found by adding up the two numbers before it. For example, the 2 is found by adding the two numbers before it (1+1). The 3 is found by adding the two numbers before it (1+2). The 5 is found by adding the two numbers before it (2+3), and so on! Each number in the sequence is called...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT