Write a MATLAB program to determine the factorial value of an input integer between 1 and 30. You may NOT use the built-in factorial function. You must implement it using a for loop. Display the result back to the user with 2 decimal places (yes, 2 decimal places). The result should follow the following format: The factorial value is *.xx
function result = facto(x)
%this function will generate factorial of a given number
%the output of the given input will be as follows:
% x(x-1)(x-2)(x-3).....(3)(2)(1)
% also if x = 0 then the value is 1 and also x is +integer
%If statement to check if the number is negative
if x<0
result = 'Negative numbers are not valid for factorial'
% else if the number is not an integer then we will convert the number to
% integer and find the value of the factorial
elseif int64(x) ~= x
fprintf('You have entered a number which is not an integer\n');
fprintf('Please enter a valid integer to find the factorial\n');
elseif x == 0
result = 1;
else
result = 1;
for i = 1:x
result = result*i;
end
end
Get Answers For Free
Most questions answered within 1 hours.