Count EVEN and ODD numbers

조회 수: 18 (최근 30일)
Jan Vergel De Ocampo
Jan Vergel De Ocampo 2021년 4월 6일
댓글: 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
Jan Vergel De Ocampo 2021년 4월 6일
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