0% found this document useful (0 votes)
33 views

DirectFileTopicDownload 2

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

DirectFileTopicDownload 2

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

COSC2006 Lab 3 – Recursion, Part 2

Coding – Upload all java files to the LMS.


The Fibonacci Sequence, Part 1. One of the most famous number sequences in mathematics,
the Fibonacci sequence, has a recursive definition. Write a recursive method to calculate the nth
Fibonacci number. In your main method, ask for n, and call your method to calculate it. Your
method signature must be public static int fib(int n).
The formula for the Fibonacci sequence is: fib(n) = fib(n – 1) + fib(n – 2), fib(1) = 1 and fib(2) = 1.

The Fibonacci Sequence, Part 2. In a new class, modify your main method code from part 1 to

F
create an array of the first n Fibonacci numbers. You will need a loop for this, but do not change

22
the recursive method! Your main method should ask for the value of n, and calculate all values
from fib(1) to fib(n). Note: fib(1) = 1 and fib(2) = 1, but you’re using array indexes!

e,
Recursive Printing. Create a recursive method, printUp(int n) to print the following pattern for
any n>0 rows: (this shows n=5).

l
so
*

on
**
***

C
****
*****
y
nn
Also create a recursive method, printDown(int n) to print the following pattern for any n>0
rows: (this shows n=5)
oh

*****
.J

****
***
of

**
Pr

*
You will need loops in your recursive methods for this question, but only to print the number of
T

asterisks. Your main method should ask for the value of n and call each method in order.
H
IG

Discussion: is it possible to write a third recursive method, if possible, called printBoth(int n) to


print both patterns at the same time, without calling printUp or printDown, or any other
R

method? The output should look like this for n=5:


PY

*
**
***
O

****
C

*****
*****
****
***
**
*
Your methods in this lab must be recursive. If they are not, you will receive a zero.
Copyright © 2021-2022 by Prof. Johnny Console, Algoma University.
All rights reserved. No parts of this work may be reproduced by any means without prior
written permission from the author.

You might also like