Generating Fibonacci Sequence Using While Loop

Hello all,
I am trying to generate the first Fibonacci Sequence Term greater than 1000 using a while loop. I am using the following code:
fibf(1) = 1;
fibf(2) = 1;
n=3:50;
while fibf(n) < 1000
fibf(n) = fibf(n-1)+fibf(n-2);
end
I am getting the error, 'Index exceeds matrix dimensions'. Any help is appreciated

 Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 5 de Oct. de 2014
fibf(1) = 1;
fibf(2) = 1;
n=3
while fibf(n-1) < 1000
fibf(n) = fibf(n-1)+fibf(n-2);
n=n+1;
end

3 comentarios

Umesh Pandey
Umesh Pandey el 19 de Ag. de 2016
Editada: Umesh Pandey el 19 de Ag. de 2016
fibf(1)=0 ? also it gives one value more than 1000.
To get values exactly less than 1000, you can change the while condition to:
while(fibf(n - 1) + fibf(n - 2) < 1000)
Austin Marking
Austin Marking el 17 de Mzo. de 2021
Does the counter variable “n” HAVE to go second in the while loop?

Iniciar sesión para comentar.

Más respuestas (1)

NEHA THAKUR
NEHA THAKUR el 2 de Abr. de 2020

1 voto

fibf(1) = 1;
fibf(2) = 1;
n=3
while fibf(n-1) < 1000
fibf(n) = fibf(n-1)+fibf(n-2);
n=n+1;
end

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 5 de Oct. de 2014

Comentada:

el 17 de Mzo. de 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by