IM STUCK HELP, HOW TO SHOW ALL THE NUMBERS ENTERED IN A LOOP BY THE USER

1 次查看(过去 30 天)
Write a script that asks the user to enter positive real numbers until their product becomes is greater than 500. The program should check if a negative number is entered, give a warning and then ask for another number. When a value of 500 is exceeded, the program should display the product of the numbers, state how many numbers were entered, and list the numbers. Use fprintf commands to display results and make the output look tidy.
I HAVE;
i=0; pro=1; while pro<500 n=input('enter a positive real number; '); while n<=0 disp('the number entered is not a positive real number'); n=input('enter a positive real number; '); end pro=pro*n; i=i+1; end disp(n) disp(pro); disp(i);

回答(1 个)

sixwwwwww
sixwwwwww 2013-12-3
Dear Merapelo, your code is working perfectly. I just made a few modifications to make output look better:
i = 0;
pro = 1;
while pro < 500
n = input('enter a positive real number: ');
while n <= 0
warning('the number entered is not a positive real number');
n = input('enter a positive real number: ');
end
pro = pro * n;
i = i + 1;
end
fprintf('Product of entered positive numbers is: %d\nThe total number of values input were: %d\n', pro, i);
Good luck!
  2 个评论
Merapelo CHamme
Merapelo CHamme 2013-12-3
But its lacking one part which im stuck on, the question requires me to show all the values that the user has input. please help
sixwwwwww
sixwwwwww 2013-12-3
try it now:
i = 0;
pro = 1;
count = 1;
while pro < 500
n = input('enter a positive real number: ');
Values(count) = n;
count = count + 1;
while n <= 0
warning('the number entered is not a positive real number');
n = input('enter a positive real number: ');
Values(count) = n;
count = count + 1;
end
pro = pro * n;
i = i + 1;
end
fprintf('Product of entered positive numbers is: %d\nThe total number of values input were: %d\n', pro, i);
fprintf('Input values are:\n')
fprintf('\t%d\n', Values)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Configure Simulation Conditions 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by