# Odd and even numbers

2.336 visualizaciones (últimos 30 días)
Ahmed Nasrullah el 22 de En. de 2016
Comentada: DGM el 24 de Mzo. de 2024
Hi I'm new in matlab so i need a little help to get started. How do i make a program which can distinguish from odd and even numbers? I know that i need to make a loop, it should be either if or while but i would love some suggestions on how to solve this problem. Thanks :)
##### 2 comentariosMostrar NingunoOcultar Ninguno
Munni el 16 de Ag. de 2023
print the values of odd number and even numbers using 2 seperate veriables in erange 10 to 120
Walter Roberson el 16 de Ag. de 2023
@Guillaume's solution already creates m_odd and m_even; the only thing left to do would be to make the m variable the integers in the desired range.

Iniciar sesión para comentar.

Guillaume el 18 de Abr. de 2023
Editada: MathWorks Support Team el 18 de Abr. de 2023
You simply have to go back to the definition of odd and even. An (integer) number is even if it is divisible by 2, odd otherwise. Divisible by 2 means that the remainder when divided by 2 is 0. That is easy to test, the function to get the remainder is (or you can use ). As with many things in matlab you do not need a loop, the functions work on vector / matrices
m = [1 2 3;4 5 6];
iseven = rem(m, 2) == 0;
Depending on the usage, you can use "rem" or "mod" to check if a number is divisible by 2. The "r = rem(a,b)" function finds the remainder after division of a by b, where the sign of the remainder r follows the sign of a. Meanwhile, the "r = mod(a,b)" function finds the remainder after division of a by b, where the sign of the remainder r follows the sign of b.
For example, if you have a matrix of integer numbers that are positive only, you can find the even or odd numbers by first finding the matrix indices of these elements using "rem":
m = [1 2 3; 4 5 6]
iseven = rem(m,2) == 0;
m_even = m(iseven)
isodd = rem(m,2) == 1;
m_odd = m(isodd)
If you want to execute some statements based on whether the positive number in m is even or odd, you can make a conditional statement within a “for” loop:

for i = 1:numel(m)
if rem(m(i),2) == 0
% statements to execute for even m(i)
else
% statements to execute for odd m(i)
end
end
Otherwise, if you have a matrix of integer numbers that can be negative in general, use "mod" instead of "rem" in the above example.
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Más respuestas (8)

Tally Miller el 5 de Nov. de 2018
you can also calculate the remainder after the division. b = mod(a,m) returns the remainder after division of a by m, where a is the dividend and m is the divisor. This function is often called the modulo operation, which can be expressed as b = a - m.*floor(a./m). The mod function follows the convention that mod(a,0) returns a.
by dividing by two, the remainder is either 1 or 0. For odd numbers, the remainder will be 1, and for even, it will be 0. Just be sure to use "==" for a true/false response when querying.
m=1
if mod(m,2) == 1
else
end
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

ElPerroVerde el 8 de Jun. de 2020
You can divide the numer by 2 and see if there's any decimal. I use this.
if floor(n/2)==n/2
% code for even
else
% code for odd
end
Note that the "floor" code returns the number without decimals. So if there's a even number, the result of n/2 will not have any decimal.
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Daniel Sharir el 9 de Abr. de 2020
if rem(n, 2) == 1
else
end
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Medical Imaging el 23 de Sept. de 2020
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; % initialization of sum of even numbers in the given list
sum_odd = 0; % initialization of sum of odd numbers in the given list
n_even = 0; % initialization of total even numbers in the given list
n_odd = 0; % initialization of total odd numbers in the given list
for i=1:1:x % start of the list
if mod(i,2)==0
sum_even=sum_even+i; % sum of the even numbers
n_even=n_even+1; % % increase sum of even number by 1
else
sum_odd=sum_odd+i; % sum of the odd numbers
n_odd=n_odd+1; % increase sum of even number by 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.
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Sebastián Salazar Giraldo el 16 de Mayo de 2021
odd numbers from 1 to 10
for n=1 :2:10
end
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Jan Siegmund el 7 de Abr. de 2020
Editada: Jan Siegmund el 7 de Abr. de 2020
m = [1 2 3;4 5 6];
isodd = bitget(m,1)
however this is slower as rem
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

JAISAI KRISHNAN el 21 de Jun. de 2022
You can use bitwise 'AND' to check if the number is odd (or) even.
vec = 1:1:10;
oddCheck = bitand(vec,1);
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Temidayo Daniel el 16 de Oct. de 2022
Editada: Walter Roberson el 24 de Mzo. de 2024
x = 1:10
Y = zeros(1,10)
for i = 1:10
if mod (x(i), 2) == 0
Y(i) = 1;
else
Y(i) = 0;
end
end
you can also switch it for odd numbers between 1 t0 10 .
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
DGM el 24 de Mzo. de 2024
Yeah, but why use loops? Why use literal numeric assignments? It can all be done in one line, for any size array, and the output is more useful, since it can directly be used for indexing.
x = 1:10
x = 1x10
1 2 3 4 5 6 7 8 9 10
isodd = logical(mod(x,2)) % true when odd
isodd = 1x10 logical array
1 0 1 0 1 0 1 0 1 0

Iniciar sesión para comentar.

### Categorías

Más información sobre Elementary Math en Help Center y File Exchange.

### Etiquetas

Aún no se han introducido etiquetas.

### Community Treasure Hunt

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

Start Hunting!

Translated by