Help with for loop
2 次查看(过去 30 天)
显示 更早的评论
The problem is write a scripts file and use a for loop to determine the sum and number of all odd integers from 1 to 100 which is divisible by 3.
the answer should look like
sum of all odd integers divisible by 3 is :
number of all odd integers divisible by 3 is:
so far i have
x=1:2:100;
b=mod(x,3)
for b=0
but I am not sure where do go from there.
0 个评论
回答(2 个)
Star Strider
2017-3-10
You’re very close. If they’re divisible by 3, then the remainder after division by 3 will be 0.
This should get you started:
x=1:2:100;
b = x(mod(x,3) == 0);
The problem statement tells you to do this in a loop, then sum the values that meet the criterion. I leave that to you.
0 个评论
Image Analyst
2017-3-10
An alternate way:
theSum = 0;
for x = startValue : stepValue : stopValue
theSum = theSum + ...?????.......
end
Can you figure out what startValue, stepValue, and stopValue are? Do you know what to add to theSum?
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!