# C Program to find Factorial of a Number

How to write a C Program to find Factorial of a Number using For Loop, While Loop, Pointers, Functions, Call by Reference and Recursion. It denoted with the symbol (!). The Factorial is the product of all numbers, which are less than or equal to that number, and greater than 0.

n! = n * (n-1) * (n -2) * …….* 1

For example, Factorial of 5 is represented as 5! = 5 *4 * 3 * 2 * 1 = 120

## C Program to Find Factorial of a Number Using For Loop

This factorial program allows the user to enter any integer value. By using this value, this C program finds Factorial of a number using For Loop.

```/* C Program to Find Factorial of a Number Using For Loop */
#include <stdio.h>

int main()
{
int i, Number;
long Factorial = 1;

printf("\n Please Enter any number to Find Factorial\n");
scanf("%d", &Number);

for (i = 1; i <= Number; i++)
{
Factorial = Factorial * i;
}

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

return 0;
}```

In this factorial program in c, we declared two integer variables i and number. We also declared one long variable Factorial and assigned a value of 1.

Although we are calculating the C factorial of an integer variable, we declared output (Factorial) as a long variable. Because when we are calculating the factorial for large integers, then the result will definitely cross the integer limit

Below C programming printf statement will ask the user to enter his/her own integer value to calculate the factorial

`printf("\n Please Enter any number to Find Factorial\n");`

The scanf statement in this C program will assign a user-entered value to the Number variable

`scanf("%d", & Number);`

For Loop: Within the For Loop, We initialized the integer I value to 1 and also (i <= Number) condition will help the loop to terminate when the condition fails.

```for (i = 1; i <= Number; i++)
{
Factorial = Factorial * i;
}```

User entered an integer in the above example for the factorial program in c is 4

First Iteration
i = 1, Factorial = 1 and Number = 4 – It means (i <= Number) is True
Factorial = Factorial * i;
Factorial = 1 *1 = 1
i++ means i will become 2

Second Iteration
i = 2, Factorial = 1 and Number = 4 – It means (i <= Number) is True
Factorial = Factorial * i;
Factorial = 1 *2 = 2
i++ means i will become 3

Third Iteration
i = 3, Factorial = 2 and Number = 4 – It means (i <= Number) is True
Factorial = Factorial * i;
Factorial = 2 *3 = 6
i++ means I will become 4

Fourth Iteration
i = 4, Factorial = 6 and Number = 4 – It means (i <= Number) is True
Factorial = Factorial * i;
Factorial = 6 * 4 = 24

i++ means i will become 5 – It means (i <= Number) is False. So, For loop will terminate.

The last printf statement will print the Factorial output of the user entered integer. Within the printf statement, the first %d refers to Number, and the second %d refers to Factorial.

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

Factorial of 4 = 24

## C Program to Find Factorial of a Number Using While Loop

This program for the factorial program in c allows you to enter any integer value. By using the value, this C program find Factorial of a Number using While Loop

```/* C Program to Find Factorial of a Number Using While Loop */
#include <stdio.h>

int main()
{
int Number, i = 1;
long Factorial = 1;

printf("\n Please Enter any number to Find Factorial\n");
scanf("%d", &Number);

while (i <= Number)
{
Factorial = Factorial * i;
i++;
}
printf("Factorial of %d = %d\n", Number, Factorial);

return 0;
}```

C factorial number using a while loop output

`````` Please Enter any number to Find Factorial
6
Factorial of 6 = 720``````

We just replaced the For loop in the above factorial program in c example with the While loop. If you don’t understand the While Loop program, please refer While Loop article here: While Loop

## C Program to Find Factorial of a Number Using Pointers

This program for factorial in c will find Factorial of a given number using pointers.

I suggest you refer Pointers in C article before this C factorial program example. It will help you to understand the Pointers and Pointer variable Concept.

```/* C Program to Find Factorial of a Number Using Pointers */
#include <stdio.h>

int main()
{
int i, Number, *P;
long Factorial = 1;

printf("\n Please Enter any number to Find Factorial\n");
scanf("%d", & Number);

P = &Number;

for (i = 1; i <= *P; i++)
{
Factorial = Factorial * i;
}

printf("\nFactorial of %d Using Normal Variable  = %d\n", Number, Factorial);
printf("Factorial of %d Using Pointer Variable = %d\n", *P, Factorial);

return 0;
}```
`````` Please Enter any number to Find Factorial
7

Factorial of 7 Using Normal Variable  = 5040
Factorial of 7 Using Pointer Variable = 5040``````

In this factorial program in c, we assigned the address of the Number variable to the address of the pointer variable. Here, P is the address of the pointer variable that we already declared (*P). As we all know, &Number is the address of the Number.

`P = &Number;`

Within the For Loop, We checked i value against the pointer variable *P instead of Number variable.

Here, *P means Value inside the pointer variable

## C Program to Find Factorial of a Number Using Functions

This factorial program allows the user to enter any integer value. User entered value will be passed to the Function we created. Within this User defined function, this C program finds Factorial of a number using For Loop.

```/* C Program to Find Factorial of a Number Using Functions */
#include <stdio.h>

long Calculate_Factorial(int);

int main()
{
int Number;
long Factorial = 1;

printf("\n Please Enter any number to Find Factorial\n");
scanf("%d", &Number);

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

return 0;
}

long Calculate_Factorial(int Number)
{
int i;
long Factorial = 1;

for (i = 1; i <= Number; i++)
{
Factorial = Factorial * i;
}
return Factorial;
}```
`````` Please Enter any number to Find Factorial
10
Factorial of 10 = 3628800``````

In this c program to find factorial of a number example, Since the function Calculate_Factorial () will return the long value as output, we assigned the function calling to long variable Factorial

`Factorial = Calculate_Factorial(Number);`

When the compiler reaches to Calculate_Factorial(Number) line in main() function of a factorial program then the compiler will immediately jump to below function:

`long Calculate_Factorial(int Number)`

We already explained LOGIC in the above program example.

The last line ends with a return Factorial Statement. It means every time we call the Calculate_Factorial () function from the main () or any sub-functions, then it will return factorial value.

## C Program to Find Factorial of a Number Using Recursion

This factorial program allows you to enter any integer value. User entered value will be passed to the Function we created. Within this User defined function, this C program find Factorial of a number Recursively.

Please Refer to Recursion in C article before this example. It will help you to understand the recursion Concept.

```/* C Program to Find Factorial of a Number Using Recursion */
#include <stdio.h>

long Calculate_Factorial(int);

int main()
{
int Number;
long Factorial = 1;

printf("\n Please Enter any number to Find Factorial\n");
scanf("%d", &Number);

Factorial = Calculate_Factorial(Number);

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

return 0;
}

long Calculate_Factorial(int Number)
{
if (Number == 0 || Number == 1)
return 1;

else
return Number * Calculate_Factorial (Number -1);
}```
`````` Please Enter any number to Find Factorial
9

Factorial of 9 = 362880``````

Within the user-defined function of this factorial program,

`long Calculate_Factorial(int Number)`

If Statement will check whether the Given Number is Equal to 0 or Equal to 1

• If the condition is TRUE, the function will return 1.
• And, if the condition is TRUE, the function will return Number * (Number -1) recursively.

In this c program to find factorial of a number example, user Entered Value = 9. It means, First If statement fails So,

Factorial = Number * Calculate_Factorial (Number -1);
= 9 * Calculate_Factorial (9 -1)
= 9 * Calculate_Factorial (8)
= 9 * 8 * Calculate_Factorial (7)
= 9 * 8 * 7 * Calculate_Factorial (6)
= 9 * 8 * 7 * 6 * Calculate_Factorial (5)
= 9 * 8 * 7 * 6 * 5 * Calculate_Factorial (4)
= 9 * 8 * 7 * 6 * 5 * 4 * Calculate_Factorial (3)
= 9 * 8 * 7 * 6 * 5 * 4 * 3 * Calculate_Factorial (2)
= 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * Calculate_Factorial (1)
= 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2
= 362880

## C Program to Find Factorial of a Number Using Call By Reference

This factorial program allows the user to enter any integer value. Instead of User entered value, the address of the variable will pass to the Function we created. Within this User defined function, this c program find Factorial of a number using For Loop.

Please Refer Call By Value and Call By Reference article before this factorial program example. It will help you to understand the difference between call by value and call by reference Concept.

```/* C Program to Find Factorial of a Number Using Call By Reference */
#include <stdio.h>

long Calculate_Factorial(int *);

int main()
{
int Number;
long Factorial = 1;

printf("\n Please Enter any number to Find Factorial\n");
scanf("%d", &Number);

Factorial = Calculate_Factorial(&Number);

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

return 0;
}

long Calculate_Factorial(int *Number)
{
int i;
long Factorial = 1;

for (i = 1; i <= *Number; i++)
{
Factorial = Factorial * i;
}

return Factorial;
}```
`````` Please Enter any number to Find Factorial
8
Factorial of 8 = 40320``````