Factorial of a Number program in C using function

Here we are going to write factorial of a number program in c using function. Previously we have done this program normally that is just factorial of a number in c. This time we will do this using function.

What is a factorial number?

A factorial number is the product of all the number from 1 to the given number.

We represent factorial by '!' .

Example - 

n! = n x (n-1) x (n-2) x ....... x 1

5! = 5 x 4 x 3 x 2 x 1 = 120

4! = 4 x 3 x 2 x 1 = 24

3! = 3 x 2 x 1 = 6

There is one exceptional case in factorial and that you must know. Factorial of 0 i.e., 0! = 1, not 0.

Now lets discuss about the approach.

First we will create a function named fact() of int return type  and it will take one integer as parameter. The integer which we are sending as parameter is the number whose factorial we want find. Then at last in the function we will return the factorial of given number. Then in main() function we will call the fact() function, and store the returned value in a variable. And atlast print the factorial. Now lets code it.

Factorial of a Number program in C using function 

#include <stdio.h>

int fact(int n){
   int f=1,i;
    for(i=1;i<=n;i++)
    {
        f=f*i;
    }
    return f;
}

int main (){
   int n;
   printf("Enter a number: ");

   scanf("%d",&n);
   int ff=fact(n);
   printf("Factorial: %d",ff);
   
   return 0;
}

Output

Enter a number: 5
Factorial = 120

Enter a number: 0
Factorial = 1

More like this 

• Armstrong Number in C using function

• Prime Number check in C using function

• Palindrome Number in C using function 

• Add two numbers in C using function 

Post a Comment

If you have any doubts, please let me know

Previous Post Next Post