Howdy readers, today you will learn **how to write a program to find the factorial of a number using a function in the C** Programming language.

The below program prompts the user to enter a positive integer, then it computes its factorial using a **user-defined function** and **for loop**.

So, without any delay, letâ€™s begin this tutorial.

**C Program to Find Factorial of a Number Using Function**

**C Program**

// C Program to Find Factorial of a Number Using Function #include <stdio.h> int fact(int ); int main(){ int num, factorial; // Asking for input printf("Enter a positive integer: "); scanf("%d", &num); // Calling out function factorial = fact(num); // Displaying result printf("The factorial of %d is: %d", num, factorial); return 0; } int fact(int n){ int i, f = 1; for (i = 1; i <= n; i++){ f = f * i; } return f; }

**Output**

Enter a positive integer: 7 The factorial of 7 is: 5040

**Explanation**

int num, factorial;

We have declared two int data type variables named ** num** and

**.**

`factorial`

// Asking for input printf("Enter a positive integer: "); scanf("%d", &num);

The user is asked to enter a positive integer. This value gets stored in the ** num** named variable.

// Calling out function factorial = fact(num);

Then, the user-defined function named ** fact** is called in the main function.

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

We have defined a function named ** fact()** which computes the factorial of a number using

**statement.**

`for loop`

Suppose the user enters a number **3**, then:

**1st For Loop Iteration:**(i = 1; i <= 3; i++), here the condition is**True**.- f = f * i = 1 * 1 = 1

**2nd For Loop Iteration:**(i = 2; i <= 3; i++), the condition is**True**.- f = f * i = 1 * 2 = 2

**3rd For Loop Iteration:**(i = 3; i <= 3; i++), the condition is**True**.- f = f * i = 2 * 3 = 6

**4th For Loop Iteration:**(i = 4; i <= 4; i++), the condition is**False**, so the program terminates and returnswhose value is`f`

**6**for this case.

// Displaying result printf("The factorial of %d is: %d", num, factorial);

The called out value which gets stored in the ** factorial** named variable is displayed on the screen using

**function.**

`printf()`

**Output**

Enter a positive integer; 7 The factorial of 7 is: 5040

**Conclusion**

I hope after reading the program and the explanation part, you understand **how to write a program to find the factorial of a number using a function in C** Programming language.

If you have any queries regarding the program, then let us know in the comment section. We will be pleased to solve all of your queries.