Count EVEN and ODD numbers

20 Ansichten (letzte 30 Tage)
Jan Vergel De Ocampo
Jan Vergel De Ocampo am 6 Apr. 2021
Kommentiert: KSSV am 6 Apr. 2021
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

Antworten (1)

KSSV
KSSV am 6 Apr. 2021
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 Kommentare
Jan Vergel De Ocampo
Jan Vergel De Ocampo am 6 Apr. 2021
Thanks a lot. Didn't even think about that.
KSSV
KSSV am 6 Apr. 2021
Thanks is accepting/ voting the answer :)

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Environment and Settings finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by