Question: 17. Display a method to compute and print Fibonacci numbers: // prints out Fibonacci numbers F(0) ~ F(n) in the same line if n >=

17. Display a method to compute and print Fibonacci numbers:

// prints out Fibonacci numbers F(0) ~ F(n) in the same line if n >= 0.

// otherwise print an error message

public static void printFib(int n) {

/* ADD your work here */

}

Fibonacci numbers are a sequence of integers in which the first two elements are 1, and each following element is the sum of the two preceding elements. The mathematical definition of each kth Fibonacci number is the following:

F(0) = 1

F(1) = 1

F(k) = F(k-1) + F(k-2), k >= 2

The first 12 Fibonacci numbers are:

1 1 2 3 5 8 13 21 34 55 89 144

Example run | Output

printFib(-4)| Error: argument must be zero or positive

printFib(0) | 1

printFib(1) | 1 1

printFib(2) | 1 1 2

printFib(9) | 1 1 2 3 5 8 13 21 34 55

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!