While Loop wont calcuate my harmonic mean?
이전 댓글 표시
I dont see any reason why my program wont calculate the harmonic mean of the inputed numbers. The formula for the harmonic mean is ((N)/((1/x1)+(1/x2)...+(1/xn)))
Here's what i got:
total=0;
cnt=0;
x=1;
while x~=0
x=input('Please input a positive number(0 to quit):');
if x<0
break;
end
if x==0
break;
end
cnt=cnt+1;
total=(1/total) + (1/x);
end
hmean=cnt/total;
if x<0
fprintf('Error - Negative Inputs Entered, Program Terminated')
else
fprintf('\nThe harmonic mean is %g',hmean)
end
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!