3. (2 pt) Write a script that enables you to calculate and display factorial of
ID: 3594049 • Letter: 3
Question
3. (2 pt) Write a script that enables you to calculate and display factorial of n, i.e, n! (https://www.mathsisfun.com/numbers/factorial.html). Let's use n 5. Note: this script should include loop statement AND "fprintf to control the loop and print the outcomes on the screen. You may need to write a script (m-file), i.e., "facto Al111B222.m" to provide Matlab/Octave command shown below. clear all % clear all the variables that you have used. n = 5; % n for factorial n your script should be given here] while (i > 1) [your script should be given here] end fprint f ('factorial of % -n, nFactorial); You need to turn in Octave (or Matlab) output by running the developed script, facto A111B222.m in the command window as given below >>facto_A111B222 factorial of 5 = 120Explanation / Answer
LOGIC : The factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n.
i.e. n! = n*(n-1)*(n-2)*(n-3)*..........2*1
Script name : facto_A111B222.m
CODE :
clear all; % clear all the variables used before
n=5;
fact=1;
i=5;
while (i>1)
fact = fact * i;
i = i - 1;
end
fprintf('factorial of %d = %d ', n, fact);
OUTPUT :
>>facto_A111B222<enter>
factorial of 5 = 120
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.