Help with for loop

2 visualizaciones (últimos 30 días)
Michael Jones
Michael Jones el 10 de Mzo. de 2017
Respondida: Image Analyst el 10 de Mzo. de 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.

Respuestas (2)

Star Strider
Star Strider el 10 de Mzo. de 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 el 10 de Mzo. de 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?

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by