for and while loops calculations with user input

3 views (last 30 days)
hello, I should calculate the sum of positive and negative numbers and number of positives of a user given input.I considered user input 3 numbers that can be positive or negative,
  • sum_pos=0;
  • num_pos=0;
  • sum_neg=0;
  • *for i=1:3
  • T=input('please enter a negative or positive number: ');
if(T>=0)
  • sum_pos=sum_pos+(T>=0);
  • disp(sum_pos)
  • num_pos=length(T>=0);
  • else
  • sum_neg=sum_neg+(T<0);
  • disp(sum_neg);
  • end
  • end
but only the input part is working.. What can be the problem and how I can change it ?
  1 Comment
Jan
Jan on 15 Nov 2015
Please do not post a qeustion multiple time. To avoid confusing the readers, delte your other question. Thanks.
Whenever you state in a forum, that something is "not working", explain exacty, what you observe.

Sign in to comment.

Answers (1)

Jan
Jan on 15 Nov 2015
Edited: Jan on 15 Nov 2015
You are almost there. Just some small modifications:
sum_pos = 0;
num_pos = 0;
sum_neg = 0;
for i=1:3
T = input('please enter a negative or positive number: ');
if T >= 0
sum_pos = sum_pos + T; % Not +(T>=0);
num_pos = num_pos + 1;
else
sum_neg = sum_neg + T;
end
end
disp([sum_pos, num_pos, sum_neg]);

Categories

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

Community Treasure Hunt

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

Start Hunting!