Develop a user defined function that will calculate a factorial of a number that
ID: 3573451 • Letter: D
Question
Develop a user defined function that will calculate a factorial of a number that a user enters. b. Create a script that will ask the user how many times they want to run the code (use that value to set a loop), then ask the user for the value and call the function that you wrote - using factorial command is NOT allowed. Write a program that will evaluate y(x) = ln 1/1 - x Use a while loop so that the program will repeat until an illegal number is entered then you need to ask the user if they really want to quit or they made a mistake and what to enter another number. (x > = 1 is considered illegal). Run the code for one legal value and one illegal. Use fprintf to display all resultsExplanation / Answer
%Problem 1
% fact.m file
function x = fact(n)
if n==0
x = 1;
else
x = n*fact(n-1);
end
% demo.m file
n = input('Enter range:');
for i=1:n
x = input('Enter number:');
fprintf('factorial of %d is %d ',x,fact(x));
end
% sample output
%Enter range:3
%Enter number:1
%factorial of 1 is 1
%Enter number:2
%factorial of 2 is 2
%Enter number:3
%factorial of 3 is 6
% Problem 2
x = input('Enter number:');
while x<1
fprintf('%d is not a valid number ',x);
x = input('Enter number:');
end
y = log(1/(1-x));
fprintf('y(%d) is %d ',x,y);
% sample output
% Enter number:0
% 0 is not a valid number
% Enter number:1.01
% y(1.01) is 4.60517
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.