フィルターのクリア

A script that writes all even numbers from 1 to 100

35 ビュー (過去 30 日間)
Íris
Íris 2014 年 9 月 24 日
回答済み: Youssef Khmou 2014 年 9 月 24 日
I want to write a script that prints all even numbers at the range [1, 100] On one hand how would I use the while loop and on the other how would I use the for loop for this script?
  1 件のコメント
Adam
Adam 2014 年 9 月 24 日
doc for
doc while
I know this is a help forum, but come on, put some effort in at least to working out how to do it yourself. In this case you need neither a for loop nor a while loop, but you seem to want to know how to use them so those are the help paages.

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

回答 (2 件)

Rick Rosson
Rick Rosson 2014 年 9 月 24 日
In addition to
doc for
doc while
please try
doc linspace
and
doc colon
  2 件のコメント
Image Analyst
Image Analyst 2014 年 9 月 24 日
and fprintf().
Rick Rosson
Rick Rosson 2014 年 9 月 24 日
編集済み: Rick Rosson 2014 年 9 月 24 日
Good point!
;-)

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


Youssef  Khmou
Youssef Khmou 2014 年 9 月 24 日
Here is a beginning using while, :
N=100;
n=1;
c=1;
while n<N
if mod(n,2)==0
E(c)=n;c=c+1;
end
n=n+1;
end
You can use fprintf on vector E to print the even values.
a fast way is better, using a vector, you cancel the value that have residue when you divide by 2:
E=1:100;
E(mod(E,2)~=0)=[]

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by