Please answer both exercise #6 when able. Thanks I’m advanced! engineering.armst
ID: 3741836 • Letter: P
Question
Please answer both exercise #6 when able. Thanks I’m advanced!engineering.armstrong.edu Write a MATLAB program that will sum positive numbers (numbers greater than or equal to zero) read in from the user until the value -999 is entered. Once the value -999 is entered, the sum of the all the positive numbers previously entered should be displayed. Hint: this is a running sum problem but you should use a while-loop since you do not know how many 6. numbers will be entered. The sentinel value (the value -999 that indicates the end) should not be added to the sum. Use the comment skeleton of Figure 3 as a starting point. % read in first number from user % while number is not -999 compute sum
Explanation / Answer
Matlab Code:
6.
sum = 0;
x = input("Enter a number: ");
while x ~= -999
if x >= 0
sum = sum + x;
end
x = input("Enter a number: ");
end
disp(sum);
4.
for i=5:5:25
fprintf('%d ', i);
end
5.
for i=25:-5:5
fprintf('%d ', i);
end
6.
x = input("Enter number 1: ");
y = input("Enter number 2: ");
for i=x:y
fprintf('%d ', i);
end
7.
x = input("Enter number 1: ");
y = input("Enter number 2: ");
if x>y:
fprintf("Invalid values!!");
else
for i=x:y
fprintf('%d ', i);
end
end