Program For Factorial in C (With Easy Explanation Example)


In this article, we will know how to find the factorial of any number in the C language.


In the programming language, we should make this program normally.
So first of all, know what is hot factorial and how to perform it through a program in C language.


    What is Factorial Number

    Program-For-Factorial-in-C

    From a program point of view, a program in which a number is inputted and all the digits up to that number are multiply shown as output is called a factorial number.

    Suppose the input number is 6 then the factorial number of 6 will be 750.
    By this, we can easily calculate and multiply all the digits up to 

    6! = 6 * 5 * 4 * 3 * 2 * 1 = 120


    In Mathematical Forum, the factorial of any number is denoted by n! 

    6! = 6 * 5 * 4 * 3 * 2 * 1 = 120
    8! = 8 * 7 * 6 * 5 * 3 * 2 * 1 = 6

    We will do this through  of programs, in C programming 

    • Use Loop for Find factorial (Increment)
    • Use Loop for Find factorial (Decrement)
    • Use recursion to find factorial

    1.Program for Factorial In C

    #include <stdio.h>
    #include <conio.h>
        void main ()
        {
    int a, b, factorial = 1;
    printf ("Enter the number \ n");//Find Factorial
    scanf ("% d", & b);
    for (a = b; a> 0; a--)
    {
    factorial = factorial * a;
    }
    printf ("Factorialorial : % d", factorial);
    getch ();
    }

    Input

    Enter the number for Factorial 6

    Output

    Factorial of En Number = 720

    Explanation


    In this program, we have taken three integer variables a, b and factorial.
    In which the value of factorial is 1.

    Then the value is entered which values to find factorial.

    Now for loop (a = b; a> 0; a--) loop is run  in which
    The condition is that the value of b will be assigned to a.

    Then a decrement of a will occur in the loop as long as a> 0.
    As long as the loop runs

    Factorial = factorial * a;


    Then the value of the factorial is printed.


    2.Program for Factorial In C


    #include <stdio.h>
    int main ()
    {
            int a, b, factorial = 1;
             printf ("Enter the number \ n");   //Find Factorial
    scanf ("% d", & b);
    for (a = 1; a <= b; a ++)
    {
    factorial = factorial * a;
    }
    printf ("Factorialorial of Enter Number =% d", factorial);
    getch ();
    }
    Input

    Enter the number 5

    Output

    Factorialorial of Enter Number = 120

    Explanation


    In this program we have taken three integer variables a, b and factorial.
    In which the value of factorial is 1.

    Then the value is entered, the value of which is to be factorial found.
    Now for loop (a = 1; a <= b; a ++)  in which
    The condition is that in which a = 1.

    Then there will be an increment of an in the loop until a <= b.
    As long as the loop runs

    Factorial = factorial * a;

    Then the value of the factorial is printed.

    3.Program For Find Factorial In C


    #include <stdio.h>
    long fact (int);
    int main ()
    {
      int x;
      printf ("Enter the number for Factorial \ n");
      scanf ("% d", & x);
      printf ("% d! =% ld \ n", x, fact (x));
      return 0;
    }

    long fact (int x)
    {
      int y;
      long z = 1;

      for (y = 1; y <= x; y ++)
        z = z * y;

      return z;
    }

    4.Program for Factorial In C Using Recursion 


    #include <stdio.h>
          
        long fact (int a)
        {
          if (a == 0)
            return 1;
          else
            return (a * fact (a-1));
        }
           
        void main ()
        {
          int n;
          long factorial;
          printf ("Enter the number for Factorial:");
          scanf ("% d", & n);
           
          factorial = fact (n);
          printf ("Factorialorial of Enter Number% d Is% ld \ n", n, factorial);
          return 0;
        }


    Recommend:

    Post a Comment

    0 Comments