• Write a for loop to print out the following with MATLAB: • 1 bottles of ( favo
ID: 3690167 • Letter: #
Question
• Write a for loop to print out the following with MATLAB:
• 1 bottles of (favorite beverage) on the wall
•2 bottles of (favorite beverage) on the wall • … counting up to 10. • For this problem, the loop variable k should go from 1 to 10, and the fprintf statement should print out k. • Now, copy your for loop and change it to count backwards from 10 down to zero • Now alter your script to count the total number of bottles on the wall for EACH of the for loops. Don’t forget to reset the total to zero between the for loops o Create a variable totalBottles before the for loop, and set it to zero o In the for loop, add the current number of bottles (k) to the total
! Hint: totalBottles will appear on both the left and right hand side of the equals sign
• Print out the total number of bottles after each of the for loop finishes
Explanation / Answer
totalBottles=0;
for i =1:1:10
fprintf('%d bottles of (favourite beverage ) on the wall',i);
totalBottles =totalBottles+i;
end
fprintf('total bottles count is:%d',totalBottles);
totalBottels=0;
for int i=10:-1:1
fprintf('%d bottles of (favourite beverage) on the wall',i);
totalBottles=totalBottles+i;
end
fprintf("total bottles is %d',totalBottles);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.