Using Matlab this is what I have so far: K=1:30; SUM=0; for I=K R3=rem(I,3); if
ID: 672749 • Letter: U
Question
Using Matlab this is what I have so far:
K=1:30;
SUM=0;
for I=K
R3=rem(I,3);
if R3==0
SUM=SUM+I;
display 'K=3 meets the search criteria'
end
R5=rem(I,5);
if R5==0
if R3==0
continue;
else
SUM=SUM+I;
display 'K=5 meets the search criteria'
end
end
end
SUM
I can't get it to add up to 225 though. I will rate! Thank you in advance for your help!!
Sottile- Fall 2015 Problem 2 (5 points) Using a for loop, sum all of the whole numbers from 1 to 30 that are evenly divisible by 3 or that are evenly divisible by 5 (if it meets both conditions, that's okay too -just print it once). Every time you find a value that meets your search criteria, use print commands to print statements to the Command Window like this: k3 meets the search criteria k-5 meets the search criteria Once you have finished iterating through the loop, print the sum of all of the values that met your search criteria. To accomplish this, pre-initialize a counter for the running sum to be 0 before you enter the loop, and then update the value every time you find a value that meets your search criteria. Hint: A total of 14 elements should meet your search criteria, yielding a final sum of 225.Explanation / Answer
sum = 0 for m = 1:30 if mod(m,3) == 0 disp(m) disp("meets criteria") sum += m elseif mod(m,5) == 0 disp(m) disp("meets criteria") sum += m end end
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.