## Introduction to Factorial in C program

The following article, Factorial in C Program provides an outline for the topmost factorial methods in C. The symbol for factorial is denoted by using this ‘! ‘ sign. For instance, the number 6 factorial is referred to as 6!. Number factorial is described as the product “of the number, and all the entries are smaller than zero and negative.” For factorial concepts, natural numbers (non-negative entities) higher than zero are used.

Let us see some examples to understand how factorial is calculated. Below we have calculated factorial for numbers 1 to 10.

- Factorial of ZERO (0!) = 1
- Factorial of one (1!) = 1
- Factorial of Two (2!) = 2*1 = 2
- Factorial of Three (3!) = 3*2*1 = 6
- Factorial of Four (4!) = 4*3*2*1 = 24
- Factorial of Five (5!) = 5*4*3*2*1 = 120
- Factorial of Six (6!) = 6*5*4*3*2*1 = 720
- Factorial of seven (7!) = 7*6*5*4*3*2*1 = 5040
- Factorial of Eight (8!) = 8*7*6*5*4*3*2*1 = 40320
- Factorial of nine (9!) = 9*8*7*6*5*4*3*2*1 = 362880
- Factorial of Ten (10!) = 10*9*8*7*6*5*4*3*2*1 = 3628800

Below is the common mathematical formula for determining the numbers ‘ n ‘ factor.

n! = n ( n – 1)( n – 2)( n – 3) ……

### Examples of Factorial in C by using the various method

In this section, we are going to discussed how factorial is calculated in C program using different methods.

#### Example 1

Factorial program in C by using the if-else statement

In an if-else statement, first, if the statement is evaluated, if the statement in it is true it will give the output and if the statement in if the condition is not true then it transfers the control to the else statement and else statement is being executed. Let us see how we can calculate factorial using if-else statement.

4.5 (3,826 ratings)

View Course

**Code**

`#include<stdio.h>`

#include<conio.h>

int main()

{

int number, i, fact = 1;

printf("Enter the positive number to find the factorial: ");

scanf("%d",&number);

// if number is negative show the error

if (number < 0)

printf("Error! You have entered negative number and Factorial for negative number does not exist.");

else

{

for(i = 1; i <= number; ++i)

{

fact *= i; // factorial = factorial*i;

}

printf("Factorial of the given number %d is %llu", number, fact);

}

return 0;

}

**Explanation of the above code**

In the above example, we have initialized three variables number i.e. I and fact. Then scan function is used to allow a user to enter the number by their wish. If the condition first checks if the given number is negative or not, if it is negative, it will execute if the statement and throw the error and stop the program.

** Output for the negative number:**

And if the given number is positive, it will transfer control to else statement and condition are given in the else statement is executed and it will calculate the factorial for a given number. The output for the positive number is as follows.

**Output for the positive number:**

#### Example 2

Factorial program in C by using the For loop

In the For loop, firstly initialization step is executed and only once in the whole program. In this step, you can initialize and declare variables for the code. After that condition is evaluated. If the condition is true then it will execute the code inside the block of For loop. If the condition is false then it will jump to the code after the For loop without executing the code of For loop.

After the For loop, the increment statement will be executed. After that again the condition will be checked. Loop will get executed if the condition is true and the loop will repeat itself i.e. the body of the loop, an increment statement, and condition. The loop ends when the condition is false.

**Code**

`#include<conio.h>`

#include<stdio.h>

int main()

{

int i, fact = 1, number;

printf("Enter the number to find the factorial: ");

scanf("%d", &number);

for(i = 1; i <= number; i++){

fact = fact * i;

}

printf("Factorial of the given number %d is %llu", number, fact);

return 0;

}

**Output:**

**Explanation of the above program**

In this program, we have initialized the variables I, fact and number. When the condition of for loop. The scan function is used to allow a user to enter the number by their wish. After that For loop will work as explained above.

#### Example 3

Factorial program in C by using recursion method

Recursion is a method where, for instance, the feature itself is called in the software factory function below. You first need to convey its answer in the recursive form to resolve an issue via resource.

**Code**

`#include<stdio.h>`

#include<conio.h>

factorial(int);

int main()

{

int number, fact;

printf("Enter the number to find the factorial:");

scanf("%d", &number);

if(number < 0)

printf("Negative integer factorial is not described.\n");

else

{

fact = factorial(number);

printf("Factorial of the given number %d is %llu ", number, fact);

}

return 0;

}

factorial(int number)

{

if (number == 0)

return 1;

else

return(number * factorial(number - 1));

}

**Output:**

#### Example 4

Factorial program in C using function

**Code**

`#include <stdio.h>`

#include<conio.h>

factorial(int);

int main()

{

int number, fact = 1;

printf("Enter the number to find the factorial: ");

scanf("%d", &number);

printf("Factorial of the given number %d is %llu", number, factorial(number));

return 0;

}

factorial(int n)

{

int c, result = 1;

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

result = result * c;

return result;

}

**Output:**

### Conclusion

In this article, we have seen how to calculate the factorial of a number in C by using conditional statements and functions. I hope this article will help you in understanding the working of factorial in C.

### Recommended Articles

This has been a guide to Factorial in C. Here we discuss factorial for numbers 1 to 10, examples of factorial in C by using the various method, formula for “n factor” with codes and outputs. You can also go through our given articles to learn more-