Count EVEN and ODD numbers

20 visualizaciones (últimos 30 días)
Jan Vergel De Ocampo
Jan Vergel De Ocampo el 6 de Abr. de 2021
Comentada: KSSV el 6 de Abr. de 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

Respuestas (1)

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

Iniciar sesión para comentar.

Categorías

Más información sobre Environment and Settings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by