Tuesday 4 March 2014

Ways to find Factorial of a Number

This is very frequent direct or indirect questions, we found To find out factorial of a number . There are Many ways to find out factorial of a number .

Factorial of a number :

N factorial =  N! = N*(N-1)*(N-2)*(N-3) ...... *3*2*1

It is basically product of all positive number greater than equal to N .
Ex:
       4! = 4*3*2*1 =24
       5!=5*4! =%*4*3*2*1 =120
      7!=7*6*5*4*3*2*1 = 5040

Note : Factorial of  0 = 0! =1

Factorial of a Number Using For LOOP :

This is easiest way to found factorial of  a Number 

you Just have to move for loop from 1 to N or From N to 1 and multiply all term .

C Code :

 // www.beginer2cs.blogspot.com  
 //factorial using for LOOP !!!!  
 #include<stdio.h>  
 main()  
 {  
      int fact=1,n,i; // Declaration  
      printf("Enter No.: ");  
      scanf("%d",&n); //taking input from user  
      for(i=n;i>=1;i--) //moving i from n,(n-1),(n-2) .... 1  
      {  
           fact=fact*i;  
      }  
      printf("\nRequre factorial : %d! = %d",n,fact);  
 }  

you can see There is very simple operation
fact = fact *i where i move from N to 1 by decrementing by 1 in each loop .

Python 3.3.0 Code :

## WWW.beginer2cs.blogspot.com  
## factorial using for loop  
n=int(input("Enter no. : "))  
fact=1  ## initializing fact to 1  
for i in range(1,n+1): #moving i from 1 to n   
  fact=fact*i  
print(fact)  

Factorial of a Number Using While LOOP :

 This is basically representation of for loop in term of while loop . concept are same as for FOR loop .

C code :

 // www.beginer2cs.blogspot.com  
 //factorial using While LOOP !!!!  
 #include<stdio.h>  
 main()  
 {  
      int fact=1,n,i=1; // Declaration  
      printf("Enter No.: ");  
      scanf("%d",&n); //taking input from user  
      while(i<=n) //it is basicaly i implement for loop using while loop  
      {  
           fact=fact*i;  
           i++;  //increament i by 1  
      }  
      printf("\nRequre factorial : %d! = %d",n,fact);  
 }  

Related Post :  Php crash course Get POST & REQUEST function

Python3.3.0 Code :

1:  ## WWW.beginer2cs.blogspot.com  
2:  ## factorial using while loop  
3:    
4:  n=int(input("Enter no. : "))  
5:  fact=1  ## intialising fact to 1  
6:  i=1  
7:  while(i<=n):  
8:    fact=fact*i  
9:    i=i+1 #incrementing i by 1  
10:    
11:  print(fact)  
12:    

Factorial of a Number Using Recursion  :

Recursion is one of very Nice feature of programming language Which can effectively reduce   line of Code .
Some time even more efficient but it also has its own limitation . Now that is your Job to find out its limitation . I can give you some hint : concentrate on Computer Architecture .

C code :

 // www.beginer2cs.blogspot.com  
 //factorial using Recursion !!!!  
 #include<stdio.h>  
   
 int fact(int n)  
 {  
     if(n==0)  
         return 1; //basically condition to break recursion;  
     else  
       return(n*fact(n-1));  
 }  
 main()  
 {  
     int n; // Declaration  
       
     printf("Enter No.: ");  
     scanf("%d",&n); //taking input from user  
       
     printf("\nRequre factorial : %d! = %d",n,fact(n));  
 }  

you can see code Is reduce to simple if and else condition .

Python3.3.0 Code :

 ## WWW.beginer2cs.blogspot.com  
 ## factorial using Recursion  
   
   
def fact(n):  
  if(n==0):  
    return(1) ## break condition for recursion  
  else:  
    return(n*fact(n-1))  
   
   
n=int(input("Enter no. : ")) #taking n as input  
print(fact(n))  
   
   

Hope You Enjoy It ... :)

1 comment:

  1. Do you have a spam issue on this site; I also am a blogger, and I was wanting to know your
    situation; many of us have developed some nice practices and
    we are looking to swap strategies with others, why not shoot me an email if interested.


    Also visit my web-site - free chat femdom

    ReplyDelete

THANKS FOR UR GREAT COMMENT

Blogger Widgets