Help with for loop

2 次查看(过去 30 天)
Michael Jones
Michael Jones 2017-3-10
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.

回答(2 个)

Star Strider
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.

Image Analyst
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?

类别

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