I can't deal with this code.Please help me:)

1 次查看(过去 30 天)
clear all;
close all;
clc;
n= input("Enter an number to calculate its fibonacci number: "))
F = [1 1]; % Initially F has two elements
for i = 1:(n-2);
F(end+1) = F(end-1) + F(end);
if n> 0:
disp("You entered a positive number.List every fibonacci number smaller or equal than the given number")
else n<0:
disp("You enteres a negative number.Please enter the positive one!")
end
  1 个评论
Kishore
Kishore 2023-7-8
clear all;
close all;
clc;
n= input("Enter an number to calculate its fibonacci number: ")
F = [1 1]; % Initially F has two elements
for i = 1:(n-2);
F(end+1) = F(end-1) + F(end);
if n> 0
disp("You entered a positive number.List every fibonacci number smaller or equal than the given number")
else n<0
disp("You enteres a negative number.Please enter the positive one!")
end
end

请先登录,再进行评论。

回答(2 个)

KSSV
KSSV 2021-6-8

Kishore
Kishore 2023-7-8
clear all;
close all;
clc;
n= input("Enter an number to calculate its fibonacci number: ")
F = [1 1]; % Initially F has two elements
for i = 1:(n-2);
F(end+1) = F(end-1) + F(end);
if n> 0
disp("You entered a positive number.List every fibonacci number smaller or equal than the given number")
else n<0
disp("You enteres a negative number.Please enter the positive one!")
end
end
  1 个评论
Walter Roberson
Walter Roberson 2023-7-8
If n is not at least 3, then the body of the for loop will not be executed, and so the test whether n<0 would not be executed.
The code also does not ensure that n is an integer.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by