Count EVEN and ODD numbers
20 views (last 30 days)
Show older comments
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
0 Comments
Answers (1)
KSSV
on 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 Comments
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!