MATLAB: About simple recursion code

recursion

this code isn't work with me. for example i want to calculate the odd number in 9 is 5 odd number
function d = oddn(n)
%this program calculate the odd numbers in a number
if n==0 | mod(n,2)==0
d=0
elseif mod(n,2)~=0
d=1
else
d=oddn(n-1)+oddn(mod(n,2))
end

Best Answer

As I understand it you want to find the number of odd numbers from 1 to n. You shouldn't need to write a function for this, you can simply write:
d=ceil(floor(n)/2);
I used floor n, to account for circumstances where n is not an integer.