**Factorial program in c **is a simple program which is c program for factorial.

**Factorial of a number: **Factorial of a number N is calculated as N!.

**Syntax: ****N!** => N*(N-1)*(N-2)….

**Example: **calculate factorial of 6

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

factorial of a number 6 is **720**.

Using this technique we implement the **factorial program in c**.

We implement **factorial program in c** in two ways as

**Iterative method****Recursive method****Using function**

### 1. Using iterative method

Factorial using for loop, this is the first method using which we implement the factorial.

In this technique, we use the iterative loop as **for loop** to iterate and multiply the number up to N-1.

## Factorial program in c using for loop

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <stdio.h> int main() { int c, n, fact = 1; printf("Enter a number to calculate its factorial\n"); scanf("%d", &n); for (c = 1; c <= n; c++) fact = fact * c; printf("Factorial of %d = %d\n", n, fact); return 0; } |

**Output :**

### 2. Using recursive method

**Recursion in c**: This is a second method using which we implement a program to find factorial.

In this technique, we recursively call the same function to find factorial.

**Recursion**: Function calling itself is referred to as recursion.

## C program for factorial using recursion

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include<stdio.h> long factorial(int); int main() { int n; long f; printf("Enter an integer to find its factorial\n"); scanf("%d", &n); if (n < 0) printf("Factorial of negative integers isn't defined.\n"); else { f = factorial(n); printf("%d! = %ld\n", n, f); } return 0; } long factorial(int n) { if (n == 0) return 1; else return(n * factorial(n-1)); } |

**Output :**

### 3. Factorial using Function in c

This is a third method using which we implement the factorial.

## Program to find factorial of a number using function

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <stdio.h> long factorial(int); int main() { int number; long fact = 1; printf("Enter a number to calculate its factorial\n"); scanf("%d", &number); printf("%d! = %ld\n", number, factorial(number)); return 0; } long factorial(int n) { int c; long result = 1; for (c = 1; c <= n; c++) result = result * c; return result; } |

**Output :**

## Leave a Reply