Sunday, 12 December 2010

Algorithm for factorial of a given number

Description:


Factorial of a number is nothing but the multiplication of numbers from a given number to 1 Ex: 5! =5*4*3*2*1= 120 

Algorithm: 

main program

Step 1: start Step 2: read n

Step 3: call the sub program fact(n) Step 4: print the f value
Step 5: stop

Sub program:

Step 1: initialize the f=1
Step 2: if n==0 or n=1 return 1 to main program. If not go to step 3 
Step 3: perform the looping operation as follows
For i=1 i<=n; i++ Step 4: f=f*i
Step 5: return f value to the main program

algorithm


No comments:

Post a Comment