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

In this tutorial** Factorial of given number** is the mathematical problem.

**Factorial of a number : **

Factorial of a number N is calculated as N!.

**Syntax to find factorial of a number is :**

factorial

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

**Example: **

Find factorial

**calculate 6 factorial**

**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.**

## Factorial Program in c

**1. Using iterative method:**

Factorial using for loop

This is 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.

**Write a program to find factorial of a number**

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 second method using which we implement program to find factorial.

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

**Recursion** : **Function calling itself is referred 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 third method using which we implement the factorial.

Function in c : Function is a block of statement.

**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 :**

**Explanation :**

1. first we taken input from user to calculate factorial

2. now we calculate the factorial of the number using the formula N! and find a factorial of a number.

3. print Factorial

## Leave a Reply