asked 216k views
4 votes
Write a MATLAB code which asks a user for an integer number and computes the factorial of that number. Use while loop to create the code.

1 Answer

4 votes

Answer:

my_int=input('Enter an integer to calculate its factorial: ');

i=1;%Counter to the while loop

r=1;%To keep the result

while i<=my_int

r=r*i;

i=i+1;

end

display('The result is:')

r

Step-by-step explanation:

The factorial is the multiplication of all positive integers from 1 to n , my_int is the value of n in the program, in a while loop you need a condition that is met until all the positive integers have been multiplied, i is the counter that allows this (starts has one and is increasing by one till it gets the value of n) and also is useful to make the multiplication and r keeps the result of each step until the condition stop being met and then the program the program leaves the loop and displays the solution.

answered
User Jcolino
by
8.6k points