Write a MATLAB program named whileadder , to Input a desired maximum value. Be s
ID: 3760861 • Letter: W
Question
Write a MATLAB program named whileadder , to
Input a desired maximum value. Be sure it is one value only
Compute a running total by ones
Return as output the last integer that can be added before the total exceeds max
For example, if input max = 60, the output should = 10, because 1+2+3+4+5+6+7+8+9+10 = 55 and adding 11 would make the sum > 60. If max = 55, then the output would be = 10.
The problem I have is not getting the output number to be correct. So when I enter the max to be 60, the number 11 is the output I keep getting. The output I need is 10. How do I achieve this?
Explanation / Answer
n = input('Enter the maximum value: ');
sum = 0;
i = 1;
while(sum + i <= n)
sum = sum + i;
i = i + 1;
end
display(i-1)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.