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

The HW6, Problem3 code is: clear; clc; n = 5; f = n; while n > 1 n = n-1; f = f*

ID: 2293193 • Letter: T

Question

The HW6, Problem3 code is:

clear; clc;
n = 5;
f = n;
while n > 1
n = n-1;
f = f*n;
end
fprintf('When f = 5, value = %i ', f)

***

The other expert gave me a code of this problem, but it is also taking non integers values like "6.7" and it is calculating factorial for that, but it should show an error message for non integers value. so just fixed that part in below code,,

I am attaching other expert code here--

4. Convert your factorial calculation from HW6, Problem 3 to a function called fact (). The function should work with vector and scalar inputs. If a negative or non-integer number is passed to the function, the function needs to assign NaN and display an error message. Use fact() with the following numbers (stored in a vector and passed as a vector): [12,0,-7,6.7,5] For each calculation (array value) a sentence should say what the factorial is, a la "The result for 12! is 479001600." or "The result for -7! is ERROR: Cannot calculate factorial for a non-positive integer." In the main program, display the factorial values and/or the NaN results.

Explanation / Answer

MATLAB code is given below.


clc;
close all;
clear all;

A = input('Enter Input in [] brackets: ');

for k = 1:length(A)
if(A(k)>=0 && mod(A(k),1)==0 )
fact = A(k);
n = fact;
while n>1
fact = fact*(n-1);
n = n-1;
end
display(['The factorial of ',num2str(A(k)),' is ',num2str(fact)]);
else
display(['The result for ',num2str(A(k)),'! is ERROR : Cannot calculate factorial for non-positive integer']);
break;
end

end

RESULT:

Enter Input in [] brackets: [2 6 5 9 -6.4]
The factorial of 2 is 2
The factorial of 6 is 720
The factorial of 5 is 120
The factorial of 9 is 362880
The result for -6.4! is ERROR : Cannot calculate factorial for non-positive integer


  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote