MATLAB: Elements of array are odd or even

evenforifodd

clear all
clc
a=round(0+9*rand(10,1));
for n=a
if mod(n,2)==0
disp(n),disp('is even')
elseif n==0
disp(n),disp('is 0')
else
disp(n),disp('is odd')
end
end
the program must check which of the numbers of array are even or odd?but it doesnt work correctly…

Best Answer

Hi Pooneh,
The following updates will perform what is looked for
clear all
clc
a=round(0+9*rand(10,1));
for n=1:length(a) % For loop from 1 to length(a), then access each element of a with n, display n
if mod(a(n),2)==0
disp(num2str(n) + " is even")
elseif n==0
disp(num2str(n) + " is 0")
else
disp(num2str(n) + " is odd")
end
end
Hope this helps.
Regards,
Sriram