Please write a MATLAB function that finds all the divisors b
Please write a MATLAB function that finds all the divisors between 2 and 20 of and integer. Your function header should be
function div=finddivisor(a)
where the input a is an integer, the output div is a vector that contains all of the divisors of a that are between 2 and 20.
For example, if the input is 42, its divisors between 2 and 20 are 2,3,6,7,14 (21 is larger than 20 so it is not included). The output should be
div=[2 3 6 7 14]
Solution
MATLAB Code:
function [div] = finddivisor(a)
j=1;
for i=2:20
r = rem(a,i);
if r==0
xx(:,j)=i;
j=j+1;
end
end
div=xx
end
sample output
>> finddivisor(42)
div =
2 3 6 7 14
