You are on page 1of 2

Experiment No: 04

Experiment Name: To find out even/odd in MATLAB.


Objective: To write a MATLAB program to determine whether a number is even or odd.

Theory: MATLAB is a numerical computing environment and programming language


that is widely used in engineering and science. It provides a variety of functions for
performing mathematical and scientific computations, as well as for data analysis and
visualization.

To determine whether a number is even or odd in MATLAB, we can use the rem()
function. The rem() function returns the remainder of the division of two numbers. If the
remainder is 0, then the number is even. Otherwise, the number is odd.

MATLAB Code:
% Function to determine whether a number is even or odd
function [isEven] = isEven(n)
isEven = (rem(n, 2) == 0);
end

% Example usage:
n = 5;
isEven = isEven(n);

if isEven
disp('The number is even.');
else
disp('The number is odd.');
end

Result:
The number is odd.

Discussion: The isEven() function is a simple but effective way to determine whether a
number is even or odd in MATLAB. It is also very efficient, as it only requires one division
operation.
We can use the isEven() function in a variety of ways. For example, we can use it to write a
program to generate a list of even or odd numbers, or to filter a list of numbers to only include
even or odd numbers.

We can also use the isEven() function to solve more complex problems. For example, we can use
it to write a program to find the prime factorization of a number, or to determine whether a
number is a perfect square.

You might also like