(Laboratory No. 3.2: Fibonacci) : Objectives

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

College of Computing Education

3rd Floor, DPT Building


Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116

[Laboratory No. 3.2: Fibonacci]


Objectives:
1. To know the loop construct in Python Programming
2. To create a program that take advantage of the loop mechanisms

Materials:
1. PC or Laptop
2. Python Package Development Kit
3. Pycharm or any IDE

Background

In programming, a function that calls by itself multiple number of times is called Recursion. It is usually defined
as value returning function.

A recursive function is any function that fulfils the condition of recursion and has to be terminated when used
in a program. It can lead to an infinite loop if a condition is not met.

Example:

5! = 5 * 4!
4! = 4 * 3!
3! = 3 * 2!
2! = 2 * 1
Or it may be simply written as,

5! = 5 * 4 * 3 * 2 * 1 = 120

Below is a program that find factorial of number using for and while loops

IT5/L Martzel P. Baste|Page 1


College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116

Recursion at work!

To know more, visit this site: (https://www.python-course.eu/recursive_functions.php)

Instructions:

1. Create class Fibonacci[Surname]


2. Problem Scenario

Network Telecom checker

In mathematics, the Fibonacci numbers, commonly denoted Fn form a sequence, called


the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from
0 and 1. That is,

IT5/L Martzel P. Baste|Page 2


College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116

and

for n > 1
Example:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89...

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 55+89 = 144

NOTE: sometimes, other books omit the 0 as part of the sequence.

(https://en.wikipedia.org/wiki/Fibonacci_number)

Create a program that accepts N (positive) integer and prints the Fibonacci sequence using
recursive function.

3. Input

Input consist of an integer input, any positive integer.

4. Constraints

1≤N≤∞

5. Output

Your program should print the number series with its total.

IT5/L Martzel P. Baste|Page 3


College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116

6. Source Codes

sum = 0
def recursive(inputNumber):
if inputNumber <= 1:
return inputNumber
else:
return(recursive(inputNumber-1) + recursive(inputNumber-2))

nterms = int(input("Enter a number: "))


if nterms <= 0:
print("Please enter a positive integer!")
else:
print("Fibonacci sequence:")
for i in range(nterms):
sum += recursive(i)
print(recursive(i),end=" ")
print("The sum is: ", sum)

7. Sample Input/Output

NOTE: Provide a screenshot and describe your observation for each action you performed based on
the item below:

 Input zero for N

When you enter zero value of a number it will display a message that you must enter a positive integer
because in the source code of the program there is a condition that it only accept positive integer.

IT5/L Martzel P. Baste|Page 4


College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116

 Input negative value for N

When you enter negative value of a number it will display a message that you must enter a positive integer
because in the source code of the program there is a condition that it only accept positive integer.

 Input any positive integer for N

When you enter any positive integer it will accept the number and compute the Fibonacci sequence and
will display the sequence as will as the sum of the numbers.

8. Submit your file with filename convention: Fibonacci[Surname]

Rules:

1. Each laboratory activity has time limit of 1:30 minutes and is due on
the day depending on the level of difficulty or constraints.
2. Each activity will only last every after 3 days and has deduction of 10
points every day from the day it was given.

IT5/L Martzel P. Baste|Page 5

You might also like