Write a user-defined function that calculates the \"double factorial\" of a posi
ID: 2848262 • Letter: W
Question
Write a user-defined function that calculates the "double factorial" of a positive odd integer (2n - 1)!! = (2n - 1)(2n - 3)(2n - 5)... (3)(1). For the function name and arguments, use y=dblfact (x) where the input argument x is the odd number whose double factorial is calculated and the output y is x!!. The function should display an error message if the input is not a positive integer. Use your function to evaluate the following:
(a) 7!! (b) 25!! (c) 12!! (d)(-13)!!
*Please read and follow the directions carefully.
Explanation / Answer
function y = dblfact (x)
if x<=0
disp('Error. Input is not a positive integer');
else
p=1;
for k=0:2:x-1
p=p*(x-k);
end
end
y=p;
>> dblfact(7)
ans =
105
>> dblfact(25)
ans =
7.9059e+12
>> dblfact(12)
ans =
46080
>> dblfact(-13)
Error. Input is not a positive integer
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.