Factorial program in c Language, A factorial program in C is one of the most basic programs that a beginner in the world of programming can learn. Factorial is a mathematical function that is used to calculate the product of all positive integers from 1 to a given number. In this blog post, we will be discussing how to write a factorial program in C.

## What is a Factorial?

A factorial is a mathematical function that is used to calculate the product of all positive integers from 1 to a given number. It is represented by an exclamation mark (!). For example, 5! (5 factorial) is equal to 5 x 4 x 3 x 2 x 1, which is equal to 120.

You can also read:Â Install CodeBlocks CompilerÂ

The factorial function is commonly used in mathematics and statistics to calculate the number of possible combinations or permutations of a set of objects.

## How to write a Factorial Program in C

Writing a factorial program in C is a simple task that can be accomplished in a few lines of code. The basic logic behind a factorial program is to use a loop to multiply each number from 1 to the given number.

Let’s take a look at a simple example of a factorial program in C:

` ````
```#include
int main()
{
int i, n, factorial = 1;
printf("Enter a number to calculate its factorial: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
factorial *= i;
}
printf("Factorial of %d is %d", n, factorial);
return 0;
}

In this program, we have declared three variables: i, n, and factorial. The variable i is used as a loop counter, n is used to store the number for which we want to calculate the factorial, and factorial is used to store the result of the calculation.

We then ask the user to enter the number for which they want to calculate the factorial using the printf and scanf statements.

After that, we use a for loop to calculate the factorial. The loop runs from 1 to n, and in each iteration, it multiplies the current value of factorial by the loop counter i. This calculation is done using the shorthand operator *=, which multiplies the current value of factorial by i and assigns the result back to factorial.

Finally, we print the result of the calculation using the printf statement.

This program will calculate the factorial of any positive integer that the user enters.

Recursive Factorial Program in C

Another way to write a factorial program in C is by using recursion. Recursion is a technique in programming where a function calls itself repeatedly until a certain condition is met.

Let’s take a look at a simple example of a recursive factorial program in C:

` ````
```#include
int factorial(int n);
int main()
{
int n;
printf("Enter a number to calculate its factorial: ");
scanf("%d", &n);
printf("Factorial of %d is %d", n, factorial(n));
return 0;
}
int factorial(int n)
{
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}

In this program, we have declared a function called factorial that takes an integer n as its argument and returns an integer value.

The factorial function uses recursion to calculate the factorial. The base case of the recursion is when n is equal to 0, in which case the function returns 1. Otherwise, it multiplies n by the result of calling the factorial function with n – 1 as its argument.

In the main function, we ask the user to enter the number for which they want to calculate the factorial using theÂ printf and scanf statements. We then call the factorial function with the user input as its argument, and print the result using the printf statement.

This program will also calculate the factorial of any positive integer that the user enters, but it uses recursion instead of a loop to perform the calculation.

Advantages and Disadvantages of Recursive Factorial Program

There are some advantages and disadvantages of using recursion to write a factorial program.

## Advantages:

- Recursive programs are often more concise and easier to read than their iterative counterparts.
- Recursive programs can be more elegant and intuitive for certain types of problems, such as mathematical functions like factorial.

## Disadvantages:

- Recursive programs can be slower and use more memory than iterative programs.
- Recursive programs can also be more difficult to debug and maintain than iterative programs.
- Recursive programs can run into issues with stack overflow if the recursion depth is too deep.

## Conclusion

A factorial program in C is a basic program that beginners in programming can learn. It is a mathematical function that is used to calculate the product of all positive integers from 1 to a given number. In this blog post, we have discussed how to write a factorial program in C using both a loop and recursion.

Using a loop is a simple and straightforward way to write a factorial program, while using recursion can be more elegant and intuitive for certain types of problems. However, recursive programs can be slower and use more memory than iterative programs, and can also be more difficult to debug and maintain.

## You can also visit my HindiÂ YouTube Channel.

I am Avinash Singh, a software engineer from the profession and have been working in the IT industry since 2009. I live in Barabanki district of Uttar Pradesh in India and completed intermediate in 2001 from Ram Sevak Inter College, a UP Board school. After inter I completed my professional degree (BCA & MCA) from Indira Gandhi National Open University, Delhi in 2008 and along with it I did many certifications to keep my knowledge up to date and the next year I started a software development company E â€“ Vision Technocraft Pvt Ltd.