I need to do an exercise where i ask the USER for positive numbers, and while the number is positive i need to keep asking the USER for positive numbers. Then i need to do a sum of those positive numbers e show how many positive numbers the USER typed. This is my code thus far:
numero=input('Enter positive number')
while numero>=0
x=input('Enter another positive number')
end
w=[numero,x];
u=numel(w);
v=sum(w);
break
end
fprintf('Total positive numbers is %f\n',u)
fprintf('The sum is %f\n',v)
The problem is i can only enter 2 numbers and then it gives me the asnwer right away .HELP

 채택된 답변

Jan
Jan 2021년 1월 22일
편집: Jan 2021년 1월 22일

0 개 추천

n = [];
while 1
x = input('Enter a positive number: ')
if x < 0 % or <= ?
break;
end
n = [n, x];
end
fprintf('Total positive numbers is %f\n', numel(n))
fprintf('The sum is %f\n', sum(n))

추가 답변 (1개)

David Hill
David Hill 2021년 1월 22일

0 개 추천

c=1;
while 1
n(c)=input('Enter positive number');
if n(c)<0
n=n(1:end-1);
break;
end
c=c+1;
end
fprintf('Total positive numbers is %f\n',numel(n));
fprintf('The sum is %f\n',sum(n));

카테고리

도움말 센터 및 File Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

질문:

2021년 1월 21일

편집:

Jan
2021년 1월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by