Using a for loop, sum all of the whole numbers from 1 to 30 that are evenly divi
ID: 672660 • Letter: U
Question
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 tune you find a value that meets your seareh criteria, use print commands to print statements to the Command Window like this: Once you have finished iterating through the loop, print the sum of all of the values that met your seareh 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 seareh criteria.Explanation / Answer
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
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.