Clear Filters
Clear Filters

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

1 view (last 30 days)
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);

Answers (1)

sixwwwwww
sixwwwwww on 3 Dec 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 Comments
Merapelo CHamme
Merapelo CHamme on 3 Dec 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 on 3 Dec 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)

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!