Impact-Site-Verification: dbe48ff9-4514-40fe-8cc0-70131430799e

Search This Blog

#Day53 #100DaysChallenge- Matlab Loops|Factorial of a Number

#Day53-Factorial of a Number
Task:

Write a code to find factorial of a  number. 
Factorial(6)
1*2*3*4*5*6
=720

Note: This code can be done using the in-built command. But for the challenge, I am trying to avoid those.
Matlab code

function factorialn = factorialofnum(n)
factorialn=1;
if n<0
disp('no factorial');
elseif n==0
disp('Factorial is 1');
else
 
for i=1:1:n
    factorialn=factorialn*i;
end

fprintf('The factorial of %d is %d',n,factorialn);
end

Sample Input and Output

>> factorialn = factorialofnum(-1)

no factorial

factorialn =

     1

>> factorialn = factorialofnum(0)
Factorial is 1

factorialn =

     1

>> factorialn = factorialofnum(6)
The factorial of 6 is 720
factorialn =

   720

Click Here for Video Description 




Live Support: t.me/matlabirawen





No comments

Popular Posts

Followers