# C Program to find Factorial of a given number

|In this C Programming example, we will implement the program to find the factorial of a given number using For-loop and recursion.

Table of Contents

## 1. Factorial number

A** **factorial of a number is the product of all the integers from 1 to that number (say n).

The factorial of any positive number is given by-

Factorial of nn! = n * (n-1) * (n-2) * (n-3) * …… * 1For example - Factorial of 66 * 5 * 4 * 3 * 2 * 1 i.e. 720.

Helpful topics to understand this program better are

- User-defined and Recursive Functions in C
- For and While Loops in C
- If else conditions in C
- Function in C

## 2. Factorial number program in C

Let’s discuss the execution(kind of pseudo-code) for the program to *find the *factorial of a given number in C.

### 2.1. C Program to find Factorial number using Loop

In this, we use `for loop`

to find the factorial of the given number in C.

- This program takes a positive integer from the user and calculates the factorial using the
`for`

loop. - The
`For-loop`

keeps on incrementing by the value by 1. - The
`For-loop`

can be easily replaced by a`while loop`

and the function will be the same as`For-loop`

.

**Note**: We can use any loop i.e. while, do while etc instead of for loop.

#include <stdio.h> int main() { int i, fact = 1, number; printf("Enter a number:"); scanf("%d", &number); for (i = 1; i <= number; i++) { fact = fact * i; } printf("Factorial of %d is: %d", number, fact); return 0; }

OutputEnter a number: 3 Factorial of 3 is: 6

### 2.2. Factorial number program using Recursion in C

In the last section, we have implemented a program using a For-loop, let’s now use the *Recursive approach* to find the factorial of a given number in C.

- Let us suppose that the user has entered a value 4.
- Initially,
`Factorial(number)`

is called from`int main()`

with 4 passed as an argument. - Then, 3 is passed to
`Factorial(number)`

from the same function, a**recursive call**is made and in each recursive call, the value of argument n**decreases by 1**. - When the value of n is less than or equal to 1, no recursion occurs and the factorial value is returned to the
`int main()`

function.

#include<stdio.h> int factorial(int x){ if (x == 0) return 1; else return(x * factorial(x-1)); } int main(){ int number; int fact; printf("Enter a number: "); scanf("%d", &number); fact = factorial(number); printf("Factorial of %d is %d\n", number, fact); return 0; }

OutputEnter a number: 4 Factorial of 4 is 24

## 3. Conclusion

In this C Programming example, we have discussed how to find the ** Factorial of a given number** via two different approaches, using

*.*

**a For-loop, and using recursion****Helpful Links**

Please follow **C Programming tutorials** or the **menu in the sidebar** for the complete tutorial series.

Also for the example C programs please refer to **C Programming Examples**.

All examples are hosted on **Github**.

**Recommended Books**

**An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding**

**Do not forget to share and Subscribe.**

* Happy coding!! *😊