BASIC = = = = = 13 1, 1, 2, 3, 5, 8, 13, 21,

7 Computer BOOK 6 First edition 2010 Thinking kills s ! BASIC Games! buying computers! Appli cations r pute Com DV D sid e In l6 Leve ...
Author: Grant Payne
3 downloads 2 Views 934KB Size
7

Computer

BOOK

6

First edition 2010

Thinking kills s !

BASIC

Games!

buying computers!

Appli cations

r

pute

Com DV D sid e

In

l6

Leve

Aim: In this lesson, you will learn:

Tejas: In our Science class we discovered that the number of petals on flowers is mostly one of the numbers in a sequence called the Fibonacci seauence.

13 petals 5 petals Moz: What are fibonacci numbers and how do you get the sequence of Fibonacci numbers? Jyoti: In mathematics, the Fibonacci numbers are the numbers in the following integer sequence: 0, 1, 1, 2,3,5,8, 13, 21 ....... and so on. Tejas: The first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two numbers. For example, the next number (shown above) is 13 + 21 = 34. Jyoti: Let us write a program to generate the Fibonacci sequence of numbers, upto 1000. Tejas: Let us first write the algorithm for generating the sequence, by trying out an example. Examples Sequence Generation

1+1=2 1+2=3 2+3=5 3+5=8 5 + 8 = 13 1, 1, 2, 3, 5, 8, 13, 21, .... Algorithm 1. 2. 3. 4.

70

(We need three variables) Let a = previous number, b = current number, c = next sequence number Ask the user to input a number upto which fibonacci numbers should be generated. Print the first two fibonacci numbers. As long as c < 1000 repeat the following. (Calculate next fibonacci number which is the sum of prev no. and current no.) Next fibonacci sequence no c = a+b If c < 1000 print the fibonacci numbers. (The previous number and the current number shift in the sequence.) previous number a = b. Current number b = c.

Tejas: The repeat statement “For” 1 For statement will not work here as we do not know the number of times that we need to repeat a set of statements. Let us see if we have other repeat statements that we can use. Jyoti: We can use “While” statement.

Syntax while condition statement(s) end while

• Do the statements in the block

i=8 while i