Fibonacci sequence with loop
显示 更早的评论
I need help with these exercise:
Write a function Fibonacci(n) that takes n as an
input and produces the corresponding Fn as an output
• Use a for loop that computes the first 10 numbers (1<= n <= 10)
• Use a while loop that computes all Fibonacci numbers below 1000 (All n that Fn<1000).
If you can also add explanations it will be perfect
2 个评论
James Tursa
2020-5-1
What have you done so far? What specific problems are you having with your code?
Lisa Fontana
2020-5-1
采纳的回答
更多回答(2 个)
Prasad Reddy
2020-5-1
clc
clear all
Fibonacci(10)
function fibn=Fibonacci(n) % we are defining a function Fibonacci
fibn=[1 1] % initialiing first two values for fibonacci series
for i=3:n % since already two values are present we are starting the loop from third element
fibn(i)=fibn(i-2)+fibn(i-1); % i th element in fibnochi series is the sum of previous two elements
end
end
8 个评论
Prasad Reddy
2020-5-1
This the how to do it in a function.
Lisa Fontana
2020-5-1
James Tursa
2020-5-1
@Prasad: Please don't provide complete solutions to homework questions.
Prasad Reddy
2020-5-1
Actually i am new to this cody community. How to identify whether a question is a Home work question or not?? i am littel confused.
Lisa Fontana
2020-5-1
Prasad Reddy
2020-5-1
@ James Tursa I am in a hurry to become a MVP in this group. I am answering as many questions as possible every day. but please say how to avoid Home work Questions.
Prasad Reddy
2020-5-1
编辑:Prasad Reddy
2020-5-1
@ Lisa Fontana Ok madam i can understand. post any questions which you dont understand, we are ready to help. It seams that this is your firt question in this community. happy learning.
Lisa Fontana
2020-5-1
Tanuj Monu
2022-1-23
function f = fib(n)
f(1) = 1;
f(2) = 1;
for i = 3:n
f(i) = f(i-1) + f(i-2);
end
f = f(end);
end
类别
在 帮助中心 和 File Exchange 中查找有关 MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!