Google+ C Program For Factorial of a Number[How to] - CodieeHome
“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.”
__

Factorial Calculation

Goal:- Program for Factorial Calculation of a Given Number.

Method-Used:-  Recursion.

Explanation:- "Recursion is a process in which a function call itself."

In Mathematics , factorial of a non negative integer n is equal to the product of all positive integer less than or equal to n.

It is simply denoted by n!.  Factorial of Zero(0) is always equal to 1.

Example:- Factorial of 6.

                                       6*5*4*3*2*1 =720.

In Our program used Factorial Function is explained below--.


int factorial(int k)              ------------->if k=6 ,then steps

 {                                                       1.   6*factorial(5) again call to factorial

  if(k==1)                                             2.  6*5*factorial(4)      

       {                                                  3.  6*5*4*factorial(3)
                                                          4.  6*5*4*3*factorial(2)
          return 1;                                   5.  6*5*4*3*2*factorial(1)

          }                                              6.  6*5*4*3*2*1=720
      else                                       This result will be returned to main as result.
        {
            printf("%d*",k);
           return k*factorial(k-1);  
        } 
    }    

Program:-

  #include<stdio.h>

 #include<conio.h>

int factorial(int k)

    {

          if(k==1 | k==0)

       {

          printf("1");

          return 1;

        }

      else

        {

                  printf("%d*",k);

                 return k*factorial(k-1);   //recursive call to factorial...

        } 

   }    

int main()

 {

      int fact,n;

      printf("\n\tEnter any No.");

      scanf("%d",&n);

      printf("\n\t");

      fact=factorial(n);

      printf("\n\n\tFactorial of Entered Number is-%d",fact);

      getch();

}     

Output:-

        

If you want to share more information about topic discussed above or find anything incorrect Please do comments.  

0 comments:

Post a Comment

 
Top