MATLAB: How to display an expression when the user enters a negative number as a first input in a while loop that stops until the user enters a negative number

displaywhile loops

The expression could be 'No average'. I have tried to add an if-statement but I did not succeed.
% Prompts the user to enter
% positive numbers until the user
% enters a negative number
% Counts the average positive numbers entered by the user
counter=0;
sum=0;
inputnum=input('Enter a positive number:');
while inputnum >= 0
counter=counter+1;
sum=sum+inputnum;
inputnum=input('Enter a positive number:');
end
avg=sum/counter;
fprintf('Your average is %.1f\n',avg)

Best Answer

% Prompts the user to enter
% positive numbers until the user
% enters a negative number
% Counts the average positive numbers entered by the user
counter=0;
sum=0;
inputnum=input('Enter a positive number:');
if inputnum>=0
while inputnum >= 0
counter=counter+1;
sum=sum+inputnum;
inputnum=input('Enter a positive number:');
end
avg=sum/counter;
fprintf('Your average is %.1f\n',avg)
else
disp('no average')
end
Related Question