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

1 vue (au cours des 30 derniers jours)
Merapelo CHamme
Merapelo CHamme le 3 Déc 2013
Commenté : sixwwwwww le 3 Déc 2013
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);

Réponses (1)

sixwwwwww
sixwwwwww le 3 Déc 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 commentaires
Merapelo CHamme
Merapelo CHamme le 3 Déc 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 le 3 Déc 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)

Connectez-vous pour commenter.

Catégories

En savoir plus sur Configure Simulation Conditions dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by