Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using your knowledge of for-end loops and/or conditional statements, write a MAT

ID: 1719619 • Letter: U

Question

Using your knowledge of for-end loops and/or conditional statements, write a MATLAB program that outputs the factorial of any positive input integer N. Recall that the factorial of a number is found by multiplying the integer of interest to all numerically previous positive integers. For example Also, recall that 0! is explicitly defined as equal to 1. Provide an error message in the command window if the input number is negative or not an integer. You may not use the factorial or similar command to complete this problem. First, test run your program with the following input, N=8 where the output would be 8! is 40320

Explanation / Answer

function[ y ] = fact( n )
if rem(n,1)==0
if n<0
y = 'not a valid number';
elseif n==0
y = 1;
else
y = 1;
for i=1:n
y = y*i;
end
end
else y = 'not a valid number';
end

Comment if it is not clear