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?

Want a Top Software Development Job? Start Here!

Full Stack Development-MEANExplore Program
Want a Top Software Development Job? Start Here!

What is Factorial? Explained With an Example

Factorial is an important concept in mathematics, and it is used in various fields, such as probability theory, combinatorics, and calculus. In programming, it is often used to solve problems related to permutations and combinations. Writing a program to calculate the factorial of a given number is a common programming task, and it is essential to understand how to do it in C.

To understand the concept of factorial better, let's consider another example. Assume we want to calculate the factorial of 8. We can write it as 8! and it can be calculated as follows:

8! = 8 x 6 x 5 x 4 x 3 x 2 x 1

= 40320

Therefore, the factorial of 8 is 40320.

Get the Coding Skills You Need to Succeed

Full Stack Development-MEANExplore Program
Get the Coding Skills You Need to Succeed

Prerequisite to Write a C Factorial Program

C Data Types

Data types in C specify the type of data that a variable can hold. There are several built-in data types in C, such as int, float, char, and double. It is essential to understand data types to declare variables in a C program correctly. In a factorial program, variables are used to store the given number and the factorial result. 

C Programming Operators

Operators in C are symbols used to perform operations on variables and values. There are several types of operators in C, such as arithmetic, relational, logical, bitwise, and assignment operators. In a factorial program, arithmetic operators are used to perform the multiplication operation to find the factorial of a given number. 

C if...else Statement

The if...else statement is used in C to execute a block of code based on a condition. It is used to check whether a given number is negative or zero in a factorial program. If the number is negative or zero, the program will display an error message.

C for Loop

The for loop is a control flow statement in C that allows a block of code to be executed repeatedly based on a given condition. In a factorial program, the for loop is used to calculate the factorial of a given number by iterating from the given number down to 1 and performing the multiplication operation.

Boost Your Coding Skills. Nail Your Next Interview

Full Stack Development-MEANExplore Program
Boost Your Coding Skills. Nail Your Next Interview

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.

Now,  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_for_Factorial_1.

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_for_Factorial_2

Preparing Your Blockchain Career for 2024

Free Webinar | 5 Dec, Tuesday | 9 PM ISTRegister Now
Preparing Your Blockchain Career for 2024

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_for_Factorial_3

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_for_Factorial_4.

Want a Top Software Development Job? Start Here!

Full Stack Development-MEANExplore Program
Want a Top Software Development Job? Start Here!

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:

C_Program_for_Factorial_5.

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_for_Factorial_6

Learn 15+ In-Demand Tools and Skills!

Automation Testing Masters ProgramExplore Program
Learn 15+ In-Demand Tools and Skills!

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_for_Factorial_7

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:

C_Program_for_Factorial_8.

C Program to Find Factorial Without Using Recursion

#include <stdio.h>

int main()

{

   int n, i, fact = 1;

   printf("Enter a number: ");

   scanf("%d", &n);

   if (n < 0)

      printf("Error! Factorial of a negative number doesn't exist.");

   else

   {

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

      {

         fact *= i;

      }

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

   }

   return 0;

}

Here's How to Land a Top Software Developer Job

Full Stack Development-MEANExplore Program
Here's How to Land a Top Software Developer Job

Choose The Right Software Development Program

This table compares various courses offered by Simplilearn, based on several key features and details. The table provides an overview of the courses' duration, skills you will learn, additional benefits, among other important factors, to help learners make an informed decision about which course best suits their needs.

Program Name Full Stack Java Developer Career Bootcamp Automation Testing Masters Program Post Graduate Program in Full Stack Web Development
Geo IN All Non-US
University Simplilearn Simplilearn Caltech
Course Duration 11 Months 11 Months 9 Months
Coding Experience Required Basic Knowledge Basic Knowledge Basic Knowledge
Skills You Will Learn 15+ Skills Including Core Java, SQL, AWS, ReactJS, etc. Java, AWS, API Testing, TDD, etc. Java, DevOps, AWS, HTML5, CSS3, etc.
Additional Benefits Interview Preparation
Exclusive Job Portal
200+ Hiring Partners
Structured Guidance
Learn From Experts
Hands-on Training
Caltech CTME Circle Membership
Learn 30+ Tools and Skills
25 CEUs from Caltech CTME
Cost $$ $$ $$$
Explore Program Explore Program Explore Program

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.

FAQs

  • How to write a code for factorial in C?

#include <stdio.h>

int main() {

   int num, i;

   unsigned long long factorial = 1;

   printf("Enter a positive integer: ");

   scanf("%d", &num);

   if (num < 0)

       printf("Error! Factorial of a negative number doesn't exist.");

   else {

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

           factorial *= i;

       }

       printf("Factorial of %d = %llu", num, factorial);

   }

   return 0;

}

This program prompts the user to enter a positive integer and calculates the factorial of the given number using a for loop. It also includes a check to ensure that the entered number is not negative.

  • How do you write a factorial form?

To write the factorial form of a number, we use the exclamation mark (!) symbol. For instance, the factorial form of the number 5 is written as 5!, which represents the product of all positive integers less than or equal to 5. The formula for calculating the factorial of a given number n is:

n! = n x (n-1) x (n-2) x ... x 2 x 1

For example, 5! can be calculated as

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

The factorial form is used to express the result of the factorial operation in a compact form. It is commonly used in mathematics and computer science to represent the results of calculations involving factorials.

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 15 Apr, 2024

6 Months$ 8,000
Automation Test Engineer

Cohort Starts: 28 Feb, 2024

11 Months$ 1,750
Full Stack Java Developer

Cohort Starts: 19 Mar, 2024

6 Months$ 2,000
Full Stack Developer Course - MERN Stack

Cohort Starts: 3 Apr, 2024

6 Months$ 1,500