Factorial of a nonnegative integer n denoted by n is the pro
Factorial of a non-negative integer n, denoted by n! is the product of all positive integers less than or equal to n. For example, 5! = 5 times 4 times 3 times 2 times 1 = 120 Write a function y=my_ factorial(n) to evaluate the factorial of any non-negative integer n. DO NOT use the built-in function \"factorial\" in your code. Note that 0! = 1. Your output should be like: >> my_ factorial (3) The factorial of 3 is 6. Calculate the following factorials for n = 0, 3, 5, 7, 12.
Solution
PROGRAM CODE:
Function for calculating factorial
%FACTORIAL Calculate the factorial of n
function y = my_factorial( n )
y=1;
for ii=1:n % calculate N!
y=y*ii;
end
fprintf(\"The factorial of %d is %d.\ \", n, y);
end
% calling the functions for each n
my_factorial(0);
my_factorial(3);
my_factorial(5);
my_factorial(7);
my_factorial(12);
OUTPUT:
