xenou
MrExcel MVP
- Joined
- Mar 2, 2007
- Messages
- 16,836
- Office Version
- 2019
- Platform
- Windows
Hi,
This is a short program to output a fibonacci sequence. Can anyone think of another way to create this algorithm? The problem is one of the questions at the end of a chapter in a book about programming. Assume you have only the most basic flow control knowledge and can only use primitive types (technically I'm not even supposed to know how to declare an array yet, either).
This is a short program to output a fibonacci sequence. Can anyone think of another way to create this algorithm? The problem is one of the questions at the end of a chapter in a book about programming. Assume you have only the most basic flow control knowledge and can only use primitive types (technically I'm not even supposed to know how to declare an array yet, either).
Code:
[COLOR="Navy"]Sub[/COLOR] FibNums()
[COLOR="Navy"]Dim[/COLOR] arr(2) [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] nextNum [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] x [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] rsp [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] msg [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="SeaGreen"]'//Seed First Two Fibonacci numbers[/COLOR]
arr(0) = 0
arr(1) = 1
[COLOR="SeaGreen"]'//User inputs how many Fibonacci numbers to list[/COLOR]
rsp = CLng(Application.InputBox("How many Fibonacci numbers do you want [enter a number zero or greater]:"))
[COLOR="Navy"]If[/COLOR] CLng(rsp) > 0 [COLOR="Navy"]Then[/COLOR]
msg = "1"
[COLOR="Navy"]For[/COLOR] x = 2 [COLOR="Navy"]To[/COLOR] CLng(rsp)
nextNum = arr(0) + arr(1)
msg = msg & " " & nextNum
arr(0) = arr(1)
arr(1) = nextNum
[COLOR="Navy"]Next[/COLOR] x
MsgBox msg
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]If[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]