MATLAB A factorial is the product of all positive integers less than or equal to
ID: 3803734 • Letter: M
Question
MATLAB
A factorial is the product of all positive integers less than or equal to a given positive number. Write a function that will receive as input either a single positive number or a vector of positive numbers and return the factorials as a vector.
For example, if 5! is passed to the function, the returned value would be 120 (1*2*3*4*5). Likewise, if a vector [6 4 2 1] is passed, the function will return a vector with the factorial of every element in the input vector. Make sure you use a for loop in your solution.
function output = fact2prod(n)
Explanation / Answer
Please Note:
factorial(n) returns the factorial of n.
factorial(A) returns the factorials of each element of A,where a is a vector of positive no.s
n = 9
% use iterations
f = 1;
for i = 1:n
f = f*i;
end
f
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.