September 27, 2022

Algorithm to Calculate factorial using function

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


Algorithm to Calculate factorial using function.

1. Read N.
2. ANS=FACT(N).
3. Write ANS.
4. Exit

[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.
6. Exit.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *