In this tutorial, we will write an algorithm to calculate factorials using the function. using this algorithm we can make a program to calculate factorial using functions in most programming languages like java python c++ and c programming.
Algorithm to Calculate factorial using function.
1. Read N.
3. Write ANS.
[This procedure is used to calculate the factorial of N]
1. Set ANS:=1
2. Repeat step 3 for I:=N to 1
3. Set ANS:=ANS*I
4. [End of Step 3 loop]
5. Return ANS.
In the above algorithm first, we write the main program algorithm and then we write the algorithm for the function then we call that function in our main program. in the above function first, we read an integer N variable and then we call the function using that variable as input and assign its output to the ANS variable and then write it to the output screen and exit from the program.
In the function that calculates the factorial, we first set the value of the ANS variable to 1 and then repeat step 3 till the value of I become 1. in the loop we set the value of ANS to ANS*I and then return that ANS and exit from the function.
Remember that when we exit from the function the cursor return to the program where it will be called.