The factorial of a positive integer n is denoted n is and n! is defined as follo
ID: 3859260 • Letter: T
Question
The factorial of a positive integer n is denoted n is and n! is defined as follows: n! = n(n - 1)(n - 2) (1) Examples: 11 = 1; 61=6 times 5 times 4 times 3 times 2 times 1 = 720 Also, by definition 0! =1 Write a MATLAB script to calculate the factorial of a user specified integer n. YOU ARE NOT ALLOWED TO USE THE BUILT-IN MATLAB FACTORIAL FUNCTION YOU ARE BEING ASKED TO WRITE YOUR OWN FACTORIAL SCRIPT Your script must use either input, disp or fprintf statements and for loops or the while construct. The syntax of the input, disp and fprintf statements is given in question 2, of this test The (basic) syntax of the for statement is: for index=l:maxindex block of statements end and the syntax of the while statement is; while (logical expression) block of statements endExplanation / Answer
Matlab Code:
n=input('Enter the number to calculate factorial:');
fact=1;
if n==0
fact=1;
else
for i=1:n
fact=fact*i;
end
end
fprintf('Factorial of %d is:',n);
disp(fact);
Output:
Enter the number to calculate factorial:10
Factorial of 10 is: 3628800
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.