In: Electrical Engineering
A matlab program to enter N numbers and count the positive, negative and zero numbers. Then print the results
The matlab program to count positve negative and zeros of the enterd no :
N=input("enter a no of elements") % asking no of
elements
arr=zeros(1,N); %intitialzing empty arry with 1 row and n no of
columns
for i=1:1:N % for loop to get the elemnts from user
k=input("enter the elements"); % asking every element in the
arry
arr(i)=k; % assigning value to arry
end % ending the for loop
fprintf(" the arry is :\n"); % printing the arry
for i=1:1:N % for loop to print the elements of the arry
disp(arr(i)) % displaying arry elements
end % ending the for loop
pos=0;neg=0;zero=0; % intitalizing the positive,negative,zeros are 0
for i=1:1:N % for loop to access elements in the
arry
if(arr(i)==0) % checking if the elment is 0 or not
zero=zero+1; % incrementing zeros if condition is true
elseif(arr(i)<0) % checking the element is negative or not
neg=neg+1; % incrementing neg if condition is true
else
pos=pos+1; % if element is not zero and negative then increments
the pos
end % ending the condition
end % ending the for loop
fprintf("the total no of positive no are : %d \n",pos);
% prints total no of pos no
fprintf("the total no of negative no are : %d \n",neg); % prints
total no of neg no
fprintf("the total no of zeros no are : %d \n",zero); % prints
total no of zeros
The ouput for the above program is :-