Help with for loop

2 Ansichten (letzte 30 Tage)
Michael Jones
Michael Jones am 10 Mär. 2017
Beantwortet: Image Analyst am 10 Mär. 2017
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.

Antworten (2)

Star Strider
Star Strider am 10 Mär. 2017
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 am 10 Mär. 2017
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?

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by