Factorial of a positive integer (number) is the sum of multiplication of all the integers smaller than that positive integer. For example, factorial of 5 is 5 * 4 * 3 * 2 * 1 which equals to 120.

**Factorial Program in C**: All positive descending integers are added together to determine the factor of n. Hence, n! is denoted as a Factorial of n.

A factorial is denoted by "!". So, suppose, you want to find the factorial of the number n, then n! = n * (n-1) * (n-2) * (n-3) â€¦ *.Â

Now, letâ€™s see how to write a C program for factorial of a number?

## Algorithm of Factorial Program in C

The algorithm of a C program to find factorial of a number is:

- Start program
- Ask the user to enter an integer to find the factorial
- Read the integer and assign it to a variable
- From the value of the integer up to 1, multiply each digit and update the final value
- The final value at the end of all the multiplication till 1 is the factorial
- End program

### Pseudocode of C Program for Factorial

Using the above algorithm, we can create pseudocode for the C program to find factorial of a number, such as:

procedure fact(num)

until num=1

fact = fact*(num-1)

Print fact

end procedure

Now that we know the basic algorithm and pseudocode to write a C program for factorial, letâ€™s start implementing it using various methods.

## C Program to Find Factorial Using For Loop

We will start by using a for loop to write a C program for the factorial of a number. The program will have an integer variable with a value of 1. The for loop will keep increasing the value by 1 with each iteration until the number is equal to the number entered by the user. The final value in the fact variable will be the factorial of the number entered by the user.

### Example:

#include<stdio.h>

int main(){

Â Â Â Â int x,fact=1,n;

Â Â Â Â printf("Enter a number to find factorial: ");

Â Â Â Â scanf("%d",&n);Â

Â Â Â Â for(x=1;x<=n;x++)

Â Â Â Â Â Â Â Â fact=fact*x;Â

Â Â Â Â printf("Factorial of %d is: %d",n,fact);

Â Â Â Â return 0;

}

#### Output:

## C Program to Find Factorial Using While Loop

In this example, we will implement the algorithm using a while loop and find the factorial program in c.

#include<stdio.h>

int main(){

Â Â Â Â int x=1,fact=1,n;

Â Â Â Â printf("Enter a number to find factorial: ");

Â Â Â Â scanf("%d",&n);

Â Â Â Â while(x<=n){

Â Â Â Â Â Â Â Â fact=fact*x;

Â Â Â Â Â Â Â Â x++;

Â Â Â Â }Â

Â Â Â Â printf("Factorial of %d is: %d",n,fact);

Â Â Â Â return 0;

}

#### Output:

## C Program to Find Factorial Using Recursion

Now, we will write a factorial program using a recursive function. The recursive function will call itself until the value is not equal to 0.

Now, we will write a C program for factorial using a recursive function. The recursive function will call itself until the value is not equal to 0.

### Example :

#include <stdio.h>

int main(){

int x = 7;

printf("The factorial of the number is %d", fact(x));

return 0;

}

// Recursive function to find factorial

int fact(int y){

if (y == 0)

return 1;

return y * fact(y - 1);

}

#### Output:

## C Program fo Find Factorial Using Ternary Operator

The ternary operator is like a shorthand for an if...else statement. It provides two conditions and the statements to be executed based on the conditions. Hereâ€™s the C program for factorial using a ternary operator.

Hereâ€™s the factorial program using a ternary operator.

#include <stdio.h>

int main(){

int n = 4;

printf("The factorial of %d is %d",

n, fact(n));

return 0;

}

int fact(int x){

// Using ternary operator

return (x == 1 || x == 0)

? 1

: x * fact(x - 1);

}

#### Output:

## C Program to Find Factorial Using tgamma() Method

The tgamma() function is a library function defined in math.h library in C programming. It computes the gamma function of the passed argument.

Letâ€™s use this function to write a C factorial program.

#include <math.h>

#include <stdio.h>

int main(){

int x = 5;

x = tgamma(x + 1);

printf("%d", x);

return 0;

}

#### Output:

Note: The tgamma() function works only for small integers as C canâ€™t store large values. Also, for integers above 5, you will have to add 1 to the final output to get the factorial.

## C Program to Find Factorial Using Function

You can also create your own function to find the factorial of a given number. The below code demonstrates the use of functions to find factorial.

### Example:

#include<stdio.h>

int findFact(int);

int main(){

Â Â Â Â int x,fact,n;

Â Â Â Â printf("Enter a number to get factorial: ");

Â Â Â Â scanf("%d",&n);

Â Â Â Â fact = findFact(n);

Â Â Â Â printf("The factorial of %d is: %d",n,fact);

Â Â Â Â return 0;

}

int findFact(int n){

Â Â Â Â int x,fact=1;

Â Â Â Â for(x=1;x<=n;x++)

Â Â Â Â Â Â fact=fact*x;

Â Â Â Â Â return fact;

}

#### Output:

## C Program to Find Factorial Using Pointers

For this example, we will create a C program to find the factorial of a number using C pointers. Below is the code to use pointers for finding factorials.

#include<stdio.h>

void findFact(int,int *);

int main(){

Â Â Â Â int x,fact,n;

Â Â Â Â printf("Enter a number to get factorial: ");

Â Â Â Â scanf("%d",&n);Â

Â Â Â Â findFact(n,&fact);

Â Â Â Â printf("The factorial of %d is: %d",n,fact);Â

Â Â Â Â return 0;

}Â

void findFact(int n,int *fact){

Â Â Â Â int x;

Â Â Â Â *fact =1;

Â Â Â Â for(x=1;x<=n;x++)

Â Â Â Â Â Â Â Â *fact=*fact*x;

}

#### Output:

## C Program to Find Factorial Series

Besides just finding factorial for a single number, we can also write a C program to find factorials of multiple numbers in a series. The below code demonstrates how to find a factorial series of numbers in a range.

#include<stdio.h>

int main(){

Â Â Â Â long fact=1;

Â Â Â Â int x,n,s_range,e_range;Â

Â Â Â Â printf("Enter the starting range: ");

Â Â Â Â scanf("%d",&s_range);Â

Â Â Â Â printf("Enter the ending range: ");

Â Â Â Â scanf("%d",&e_range);Â

Â Â Â Â printf("Factorial series of the given range is: ");

Â Â Â Â for(n=s_range;n<=e_range;n++){

Â Â Â Â Â Â Â Â fact=1;Â

Â Â Â Â Â Â Â Â for(x=1;x<=n;x++){

Â Â Â Â Â Â Â Â Â Â Â Â fact=fact*x;

Â Â Â Â Â Â Â Â }Â

Â Â Â Â Â Â Â Â printf("%ld ",fact);

Â Â Â Â }

Â Â Â Â return 0;

}

#### Output:

Earn upto 25 CEUs from Caltech CTME and score a new job with an average annual package of 9-10 L after completing the PGP in Full Stack Web Development. Enroll Today!

## Conclusion

In this article, you have learned how to write C programs to find the factorial of a number in different ways. The C factorial program is one of the essential programs in C that is used as an introduction to various concepts such as loops, operators, and functions. You can now use the programs to find factorial of any number.

If you want to sharpen your C programming knowledge, you can sign up on our SkillUp platform. The platform offers multiple free courses to help you get a clear understanding of the fundamentals of numerous programming languages, including C. However, if you want to excel in software development, it is crucial to learn multiple languages instead of sticking to one. Our Post Graduate Program in Full Stack Web Development provides applied learning materials to help you get acquainted with various programming concepts and tools to become a great software developer.