The program:

! The Fibonacci series is a mathematical series of numbers. In this series, the

! next term is the sum of the previous two terms in the series. For example, if

! the first term is 1 and the second term is 1, then the third term is (1+1) or 2

! and the fourth term is (1+2) or 3.

! Ask a user to enter the number of terms (or numbers) in the series and then

! display the values of all the terms. Assume a value of 1 for each of the first two

! terms.

! Test your program using a Fibonacci series of 2,5 and 13 terms.

! Watch the calculation with do trace, fast (n,series$) ,^j

 

! Pseudocode:

! The series begins as 1,1, so Fib(1)=1, Fib(2)=1,1, Fib(3)=1,1,2, Fib(4)=1,1,2,3,

! Fib(5)=1,1,2,3,5, Fib(6) =1,1,2,3,5,8, etc.

! Write a subroutine Fib(n) to produce the required series from an input n

! First try out the program with user input.

! Then comment out the input statements and replace with a data statement

! for 2,3,13

 

INPUT prompt "Enter an integer ": n ! the length of series required

! PRINT "The integer n is ";n

CALL Fib(n,series$)

PRINT "The Fibonacci series for ";" ";n;" "; "terms";" ";"is"

PRINT series$ !The variable returned by the subroutine

END

 

!Pseudocode:

! compute the value of last term in series

! compute the value of the next-to-last term

! posr("target$","pattern$",position)

! last position = posr(",")-1 , the position left of the last comma

! first position = posr(fibn$,",",posr(",")-1)+1 is just right of the

! next to last comma

! target$ = fibn$ , the Fibonacci series

! pattern$ = ","

!PRINT fibn$

 

 

SUB Fib(n,fibn$) ! The subroutine to calculate the series of length n

IF n=1 then ! n=1

LET fibn$="1"

ELSEIF n=2 then

LET fibn$="1,1" !n=2

ELSEIF n> 2 then

END IF

LET fibn$ = "1,1" ! initialize series

LET index=0 ! initialize index for n >2

DO until index >= n-2 ! build up the series to size n

LET index = index + 1

LET last$ = fibn$[posr(fibn$,",")+1:len(fibn$)]

LET last = val(last$) ! the last value of the series

LET position = posr(fibn$,",",len(fibn$))

!PRINT "Position of last comma is ... ";position

LET nlpos = posr(fibn$,",",posr(fibn$,",",len(fibn$))-1)

!PRINT "Position of next-to-last comma is ...";nlpos

! now find the next to last value of the series

LET nextlast$ = fibn$[nlpos+1:position-1]

!PRINT "The string value is ...";nextlast$ !check the string

LET nextlast = val(nextlast$) !convert string to number

LET next = last + nextlast ! the next term in the Fibronacci series

! next is the sum of the last and next-to-last terms

LET fibn$ = fibn$&","&str$(next) ! create the next series

! concatinate next to previous Fibonacci series

! the series builds up to length n and returns fibn$ to main program

LOOP

END SUB

 

The output of the program:

Enter an integer 2

The Fibonacci series for 2 terms is

1,1

Enter an integer 5

The Fibonacci series for 5 terms is

1,1,2,3,5

Enter an integer 13

The Fibonacci series for 13 terms is

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

Enter an integer 100

The Fibonacci series for 100 terms is

1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,2

8657,46368,75025,121393,196418,317811,514229,832040,1346269,2178309,3524578,5702

887,9227465,14930352,24157817,39088169,63245986,102334155,165580141,267914296,43

3494437,701408733,1134903170,1836311903,2971215073,4807526976,7778742049,1258626

9025,20365011074,32951280099,53316291173,86267571272,139583862445,225851433717,3

65435296162,591286729879,956722026041,1.54801e+12,2.50473e+12,4.05274e+12,6.5574

7e+12,1.06102e+13,1.71677e+13,2.77779e+13,4.49456e+13,7.27235e+13,1.17669e+14,1.

90392e+14,3.08061e+14,4.98453e+14,8.06514e+14,1.30497e+15,2.11148e+15,3.41645e+1

5,5.52793e+15,8.94438e+15,1.44723e+16,2.34167e+16,3.7889e+16,6.13057e+16,9.91947

e+16,1.605e+17,2.59695e+17,4.20195e+17,6.7989e+17,1.10008e+18,1.77997e+18,2.8800

5e+18,4.66002e+18,7.54007e+18,1.22001e+19,1.97402e+19,3.19403e+19,5.16805e+19,8.

36208e+19,1.35301e+20,2.18922e+20,3.54223e+20