Q:
MATLAB Help:
Write a program that will collect numbers from the user until the user enters a non-numeric value. It then returns the sum and the average of all the numbers entered. Example below:
-----
Enter a number: 2.5
Enter a Number: 2
Enter a Number: 59
Enter a Number: 2
Enter a Number: 9
Enter a Number: 1.1
Enter a Number: k
The sum of the 6 numbers entered is 75.6. The average is 12.6.
Solution:
result = 0;
count = 0;
while(1)
val = input("Enter a number: ",'s')
val = str2double(val)
if(val == NaN)
break;
else
result = result + val;
end
count = count + 1;
end
fprintf("The sum of the %d numbers entered is %f. The average is %f.\n",count,result, (result/count));
MATLAB Help:
Write a program that will collect numbers from the user until the user enters a non-numeric value. It then returns the sum and the average of all the numbers entered. Example below:
-----
Enter a number: 2.5
Enter a Number: 2
Enter a Number: 59
Enter a Number: 2
Enter a Number: 9
Enter a Number: 1.1
Enter a Number: k
The sum of the 6 numbers entered is 75.6. The average is 12.6.
Solution:
result = 0;
count = 0;
while(1)
val = input("Enter a number: ",'s')
val = str2double(val)
if(val == NaN)
break;
else
result = result + val;
end
count = count + 1;
end
fprintf("The sum of the %d numbers entered is %f. The average is %f.\n",count,result, (result/count));
No comments:
Post a Comment