Help with for loop
2 views (last 30 days)
Show older comments
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 Comments
Answers (2)
Star Strider
on 10 Mar 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.
0 Comments
Image Analyst
on 10 Mar 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?
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!