필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
Merapelo CHamme
Merapelo CHamme 2013년 12월 3일
댓글: sixwwwwww 2013년 12월 3일
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에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by