Count EVEN and ODD numbers

17 次查看(过去 30 天)
Jan Vergel De Ocampo
评论: KSSV 2021-4-6
I have a problem where i needed to identify 5 input number whether they are EVEN or ODD which I am able to do. The problem is I needed the Total Count of the EVEN and ODD which is the thing that I do not know.
This is the code for inputs. I just need the code for the Total Count.
a = input("Please enter 1st number: ");
if mod(a,2)==0
disp("The 1st number is EVEN");
else
disp("The 1st number is ODD");
end
b = input("Please enter 2nd number: ");
if mod(b,2)==0
disp("The 2nd number is EVEN");
else
disp("The 2nd number is ODD");
end
c = input("Please enter 3rd number: ");
if mod(c,2)==0
disp("The 3rd number is EVEN");
else
disp("The 3rd number is ODD");
end
d = input("Please enter 4th number: ");
if mod(d,2)==0
disp("The 4th number is EVEN");
else
disp("The 4th number is ODD");
end
e = input("Please enter 5th number: ");
if mod(e,2)==0
disp("The 5th number is EVEN");
else
disp("The 5th number is ODD");
end

回答(1 个)

KSSV
KSSV 2021-4-6
clc; clear all ;
a = input("Please enter 1st number: ");
nE = 0 ; nO = 0 ;
if mod(a,2)==0
disp("The 1st number is EVEN");
nE = nE+1 ;
else
disp("The 1st number is ODD");
nO = nO+1 ;
end
b = input("Please enter 2nd number: ");
if mod(b,2)==0
disp("The 2nd number is EVEN");
nE = nE+1 ;
else
disp("The 2nd number is ODD");
nO = nO+1 ;
end
c = input("Please enter 3rd number: ");
if mod(c,2)==0
disp("The 3rd number is EVEN");
nE = nE+1 ;
else
disp("The 3rd number is ODD");
nO = nO+1 ;
end
d = input("Please enter 4th number: ");
if mod(d,2)==0
disp("The 4th number is EVEN");
nE = nE+1 ;
else
disp("The 4th number is ODD");
nO = nO+1 ;
end
e = input("Please enter 5th number: ");
if mod(e,2)==0
disp("The 5th number is EVEN");
nE = nE+1 ;
else
disp("The 5th number is ODD");
nO = nO+1 ;
end
fprintf('there are %d number of Even Numbers\n',nE)
fprintf('there are %d number of Odd Numbers\n',nO)
  2 个评论
Jan Vergel De Ocampo
Thanks a lot. Didn't even think about that.
KSSV
KSSV 2021-4-6
Thanks is accepting/ voting the answer :)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by