how to write a matlab code that sum all nonzero elements that divisible by 5 in a vector

Write a MATLAB script that inputs a vector V and then displays the sum and product of nonzero elements of V that are divisible by 5.

1 件のコメント

Rik
Rik 2018 年 5 月 6 日
You should try to solve this problem step by step. What did you try so far? What code did you try?
You can find guidelines for posting homework on this forum here.

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

回答 (1 件)

JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH 2018 年 5 月 6 日
編集済み: JESUS DAVID ARIZA ROYETH 2018 年 5 月 6 日
V=input('input vector: ');
elements=V(and(V~=0,mod(V,5)==0));
fprintf(' sum = %4.2f\n product=%4.2f\n',sum(elements),prod(elements))
execution:
input vector: [5 0 10 2 8] sum = 15.00 product=50.000

2 件のコメント

You forgot the equals zero statement
mod(V,5)==0
Also I don't really see how this will help the asker in any way. You didn't explain anything you just gave the answer.
Rik
Rik 2018 年 5 月 6 日
It's generally not the best idea to just give people answers to their homework questions. They won't learn anything, might hand it in as their own work without modification, they will be back for more easy answers, and the benefit to the community as a whole is virtually zero.

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

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2018 年 5 月 6 日

コメント済み:

Rik
2018 年 5 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by