# program of factorial in c

Hello and Welcome to www.TechWithCode.com

Today we will learn about how to write a C Program to find the factorial of a number. Before moving forward, we must have knowledge about what factorial is?

## What is Factorial?

Factorial of n is the product of all positive descending integers. Factorial of n is denoted by n!. For example:

5! = 5*4*3*2*1 = 120

3! = 3*2*1 = 6

Summary: Factorial is represented using '!', so five factorial will be written as (5!),n factorial as (n!).

n! = n*(n-1)*(n-2)*(n-3)...3.2.1 and zero factorial is defined as one i.e. 0! = 1.

There can be many ways in programming to find the factorial of any number but today we will discuss two important way with the source code

## 1. Factorial Program using loop

#### Problem Statement⤵️

Write a C program that calculates the factorial of any number, this number should be taken as input from the keyboard.

#### Source code⤵️

``````
#include <stdio.h>

int main()
{
int c, n, fact = 1;

printf("Enter a number to calculate it's factorial\n");
scanf("%d", &n);

for (c = 1; c <= n; c++)
fact = fact * c;

printf("Factorial of %d = %d\n", n, fact);

return 0;
}
``````

## 2. Factorial Program using recursion

#### Problem Statement⤵️

Write a C program that calculates the factorial of any number using recursion, this number should be taken as input from the keyboard.

#### Source code⤵️

``````
#include<stdio.h>
int fact(int);
int main(){
int num,f;

printf("\nEnter a number: ");
scanf("%d",&num);

f=fact(num);

printf("\nFactorial of %d is: %d",num,f);
return 0;
}

//recursive function
int fact(int n){
if(n==1)
return 1;
else
return(n*fact(n-1));
}

``````