Computer Science, asked by kirezilael, 5 months ago

write an algorithm to calculate and display the factorial of number

Answers

Answered by kochedaksh06
2

Here I am not going to write some code because you havent mentioned about the "programming language".

Basically for factorial you just have to multiply all the numbers from 1 to the given number which is just a simple paper-pencil technique.

So, the algorithm for the factorial goes like this:

input a number n

set variable final as 1

final <= final * n

decrease n

check if n is equal to 0

if n is equal to zero, goto step 8 (break out of loop)

else goto step 3

print the result final

(if you dont want to use the input variable for decrement, you can just make a copy of it in another variable)

Also, Keep in mind that factorial of zero is 1. So, you have to handle this case separately just after entering the input and before going through that loop.

This was basically an iterative approach. However, you can implement this using recursion but I wont suggest you to use the recursion method for this very special case.

Similar questions