How do you write a function that adds all odd numbers between 2 even numbers?

For my homework i have to make a function of codes that add odd number between 2 even numbers but i have been struggling. For example Even Numbers = {2, 10}
So the odd numbers to be added would be; 3 + 5 + 7 + 9 = 24
and it's due tomorrrow. So help would be much appericated.

1 件のコメント

Dyuman Joshi
Dyuman Joshi 2023 年 9 月 4 日
MATLAB Answers does not entertain Homework questions.
If you need help, show what you have done and ask a specific question, where you are having trouble.

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

回答 (2 件)

Bruno Luong
Bruno Luong 2023 年 9 月 4 日
編集済み: Bruno Luong 2023 年 9 月 5 日
a = 2;
b = 10;
assert(mod(a,2)==0 && mod(b,2)==0 && a<=b, 'incorrect a or b')
s = (b^2-a^2)/4
s = 24

1 件のコメント

Matt J
Matt J 2023 年 9 月 5 日
編集済み: Matt J 2023 年 9 月 5 日
Or, instead of assert(),
validateattributes([a;b],'numeric',["even","nondecreasing"])

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

Matt J
Matt J 2023 年 9 月 4 日
As a hint, it is very easy to create a vector of consecutive odd numbers, e.g.,
x=(5:2:11)
x = 1×4
5 7 9 11

1 件のコメント

Torsten
Torsten 2023 年 9 月 4 日
... and it's easy to sum them with the "sum" command.

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

カテゴリ

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

製品

リリース

R2023a

タグ

質問済み:

2023 年 9 月 4 日

編集済み:

2023 年 9 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by