Filter löschen
Filter löschen

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

4 Ansichten (letzte 30 Tage)
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);

Antworten (1)

sixwwwwww
sixwwwwww am 3 Dez. 2013
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 Kommentare
Merapelo CHamme
Merapelo CHamme am 3 Dez. 2013
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 am 3 Dez. 2013
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)

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Configure Simulation Conditions finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by