The factorial program in C: Factorial is a product of integers and all the integers below it.
Factorial of a number: Factorial is represented by the symbol ‘!’. Factorial of a number N is calculated as N!.

Syntax to calculate factorial of a number: N! => N*(N-1)*(N-2)….
Example: calculate factorial of 6
6! =6*5*4*3*2*1 =720
The factorial of a number 6 is 720.

Page Contents

## Algorithm to find factorial of a number

1. Input Number N.
2. Calculate factorial of a number using formula
fact = N! (N*N-1*N-2*N-3*N-4*N-5)
3. Print factorial

## Flowchart to find Factorial of number

Using this mathematical formula we implement the factorial program in c.
We implement the factorial program in C in three different way as follows:

1. Iterative method
2. Recursive method
3. Using function

## Factorial using an 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

```#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 :

##  Factorial using recursive

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

```#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 :

##  Factorial using Function in c

This is a third method using which we implement the factorial.
Function: Function is a block of code or set of instructions that are enclosed within { opening and } closing braces.

### Program to find factorial of a number using a function

```#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 : 