how to find the summation for the odd and even numbers(from 0 to 100) save odd number in sum1 and even in sum2

43 次查看(过去 30 天)
using for loop not while
  4 个评论
Geoff Hayes
Geoff Hayes 2019-3-10
yazan - we have all been new to MATLAB at one time...but that shouldn't be a reason to not try do your homework. Please make an attempt at coding up a solution for this problem. Since you have to use a for loop (and not a while loop) see for loop and mod (which can be used to determine if a number is even or odd).

请先登录,再进行评论。

回答(1 个)

Raghunandan V
Raghunandan V 2019-3-11
编辑:Raghunandan V 2019-3-12
tryThis:
N=100;
Numbers = [1:1:N];
Even = Numbers((mod(Numbers,2) ==0));
Sum1 = sum(Even);
odd = Numbers((mod(Numbers,2) ==1));
sum2 = sum(odd);

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by