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.
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 returns to the program where it will be called.
Also, Read
- Algorithm to Find Maximum of Two Numbers
- Algorithm to Find Maximum of Three Numbers
- Algorithm to Input percentage From user and Print grade
- Algorithm to Print the Series 1 to N using for loop
- Algorithm to Print the Series 1 to N using while loop
- Algorithm to Print the Series 1 to N using Do While loop
- Algorithm to Reverse a given Number
- Algorithm to Find Whether a Number is Prime or Not
- Algorithm for Linear Search in Array