An
even number
is a number which has a
remainder of
0 upon
division by
2, while an
odd number
is a number which has a remainder of
1
upon division by
2.
If the units digit (or ones digit) is 1,3, 5, 7, or 9, then the number is called an
odd number
, and if the units digit is 0, 2, 4, 6, or 8, then the number is called an
even number (for the set of numbers 0-9). Thus, the set of
integers
can be partitioned into two
sets based on parity:
-
the set of
even
(or parity 0) integers
-
the set of
odd
(or parity 1) integers.
**Parity is a fundamental property of integers, and many seemingly difficult problems can be solved by making parity arguments.
The below function is to find the details of even and odd number in a given list
function
[sum_even,sum_odd,n_even,n_odd] = even_odd(x)
sum_even = 0;
sum_odd = 0;
n_even = 0;
n_odd = 0;
for
i=1:1:x
if
mod(i,2)==0
sum_even=sum_even+i;
n_even=n_even+1;
else
sum_odd=sum_odd+i;
n_odd=n_odd+1;
end
end
Example set for list:1,2,3,4,5,6,7,8,9,10
sum_even = 30, which is 2+4+6+8+10 = 30
sum_odd = 25 , which is 1+3+5+7+9 = 25
n_even = 5, which are 2,4,6,8,10
n_odd = 5, which are 1,3,5,7,9
I hope this helps.