Program that displays the first 20 even numbers beginning with 100?

5 ビュー (過去 30 日間)
OngoingKestrel
OngoingKestrel 2012 年 3 月 9 日
編集済み: OngoingKestrel 2021 年 1 月 5 日
I understand the function, but am having trouble reaching the output of the function using MatLab. Here's what I have so far...
function [results] = Even_Closed_Interval (num,numEven)
% Even Number Counter
% Author: OGK
% 09 March 2012
% Counts next 20 even numbers beginning with 100
num = 100;
count = 0;
numEven = 0;
a = mod (x,y)
while count < 20;
if mod == 0;
disp (num)
count = count+1;
end
num = num+1;
end
Am I on the right track? Please respond ASAP if anyone can help me.
OngoingKestrel
MATLAB User
  1 件のコメント
Oleg Komarov
Oleg Komarov 2012 年 3 月 10 日
Are you specifically asked to use a loop?

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2012 年 3 月 10 日
I think this would be more on the right track
count = 20;
startingValue = 100;
out = startingValue : 2 : startingValue +2*count-1

その他の回答 (2 件)

James Tursa
James Tursa 2012 年 3 月 9 日
Close. But:
1) You need the mod function inside your while loop.
2) You need mod(num,2) instead of mod(x,y)
3) You don't need numEven

bym
bym 2012 年 3 月 9 日
mod()
is a function, but you are comparing a variable called mod to zero
mod ==0
it would be better to do something like
while count < 20
if mod(num,2)==0
disp(num)
end
pause(1)
count = count+1;
num = num+1;
end
  1 件のコメント
James Tursa
James Tursa 2012 年 3 月 10 日
You need to have count = count + 1 inside the if test.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangePerformance and Memory についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by