This post presents multiple **C program** for calculating the **factorial of a number**, using different approaches such as **recursion, function, for loop, and while loop**.

The programs are designed to handle non-negative integers and to correctly calculate the factorial of 0 and 1.

The post provides explanations for each approach and code examples, along with sample outputs for the programs.

These C programs offer flexibility in choosing the most appropriate method for solving specific problems. Regardless of the approach chosen, these programs provide a good foundation for understanding the basic concepts involved in calculating factorials in **C programming**.

A C program for factorial is a program that calculates the **factorial of a given integer number**. Factorial of a number is the product of all the positive integers from 1 to that number.

For example, the **factorial of 5 is 5x4x3x2x1 = 120**. The program can be **implemented using a loop** or a **recursive function**. The loop iterates from 1 to the given number, and the product of all the integers in **the loop is the factorial**.

The **recursive function** calls itself with the decremented value of the input number until the base case is reached. The base case is usually when the **input number is 0 or 1, and the factorial value is 1**.

### The output of the program is the factorial value of the input number.

- Factorial is a mathematical operation that is often used in probability and combinatorics. It is represented by the symbol “!” and is defined as the product of all positive integers from 1 to n, where n is the given integer number.
- The factorial function is defined recursively as follows:
- Base case: factorial of 0 is 1
- Recursive case: factorial of n is n times factorial of (n-1)

- In C programming, a program to calculate the factorial of a number can be implemented using a loop or a recursive function.
- The loop approach is generally simpler and more efficient, as it does not require the overhead of function calls. The loop iterates from 1 to the given number, multiplying each integer by the previous result to get the factorial value.

Here’s an example for

## c program for factorial using recursion

This program prompts the user to enter a non-negative integer, and then calls the `factorial`

function to calculate the factorial of that number. The ** factorial function** is defined recursively, which means that it calls itself to calculate the factorial of smaller and smaller numbers until it reaches the base case of 0, at which point it returns 1.

Let’s go through the ** factorial function** in more detail. First, it checks if the input

`n`

is equal to 0. If it is, then it returns 1, because the factorial of 0 is defined to be 1. If `n`

is not equal to 0, then it calculates the factorial of `n-1`

by calling `factorial(n-1)`

, and multiplies that result by `n`

to get the factorial of `n`

.For example, let’s say the user enters 5 as the input. The `factorial`

function will be called with `n=5`

, and will calculate `5 * factorial(4)`

. To calculate `factorial(4)`

, the function will call itself with `n=4`

, and will calculate `4 * factorial(3)`

. This process will continue until the base case of `n=0`

is reached, at which point the function will start returning values: `factorial(0)`

will return 1, `factorial(1)`

will return 1 * 1 = 1, `factorial(2)`

will return 2 * 1 = 2, `factorial(3)`

will return 3 * 2 = 6, `factorial(4)`

will return 4 * 6 = 24, and finally `factorial(5)`

will return 5 * 24 = 120. Therefore, the program will output “Factorial of 5 is 120”.

```
#include <stdio.h>
int factorial(int n);
int main() {
int n;
printf("Enter a non-negative integer: ");
scanf("%d", &n);
printf("Factorial of %d is %d\n", n, factorial(n));
return 0;
}
int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n-1);
}
}
```**output**
**Enter a non-negative integer:** 5
**Factorial of 5 is 120
**

## factorial program in c using function

The program prompts the user to enter a non-negative integer, and then calls the `factorial`

function to calculate the factorial of that number.

The `factorial`

function takes an integer `num`

as an argument and returns the factorial of that number. The function initializes a variable `fact`

to 1, and then uses a `for`

loop to multiply `fact`

by each integer from 1 up to and including `num`

. Finally, the function returns the value of `fact`

.

For example, if the user enters 5 as input, the `factorial`

function multiplies `fact`

by each integer from 1 to 5 in turn, and then returns the final value of `fact`

, which is 120. Therefore, the program outputs “Factorial of 5 is 120”.

```
#include <stdio.h>
int factorial(int num);
int main() {
int num;
printf("Enter a non-negative integer: ");
scanf("%d", &num);
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}
int factorial(int num) {
int fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
return fact;
}
```**OUTPUT**
Enter a non-negative integer: 5
Factorial of 5 is 120

Read more :- C PROGRAM TO GENERATE THE FIBONACCI SERIES

## factorial program in c using for loop

The program prompts the user to enter a non-negative integer, and then uses a `for`

loop to calculate the factorial of that number.

The `for`

loop initializes a variable `i`

to 1, and then iterates over the loop as long as `i`

is less than or equal to `num`

. In each iteration of the loop, the variable `fact`

is multiplied by `i`

, and then `i`

is incremented by 1. Finally, the loop exits, and the program outputs the value of `fact`

.

For example, if the user enters 5 as input, the `for`

loop multiplies `fact`

by each integer from 1 to 5 in turn, and then outputs the final value of `fact`

, which is 120. Therefore, the program outputs “Factorial of 5 is 120”.

```
#include <stdio.h>
int main() {
int num;
int fact = 1;
printf("Enter a non-negative integer: ");
scanf("%d", &num);
for (int i = 1; i <= num; i++) {
fact *= i;
}
printf("Factorial of %d is %d\n", num, fact);
return 0;
}
```**output**
Enter a non-negative integer: 5
Factorial of 5 is 120

read more :- C PROGRAM TO CHECK WHETHER A NUMBER IS AN ARMSTRONG NUMBER OR NOT

## factorial program in c using while loop

The program prompts the user to enter a non-negative integer, and then uses a `while`

loop to calculate the factorial of that number.

The `while`

loop initializes a variable `i`

to 1, and then continues to iterate over the loop as long as `i`

is less than or equal to `num`

. In each iteration of the loop, the variable `fact`

is multiplied by `i`

, and then `i`

is incremented by 1. Finally, the loop exits, and the program outputs the value of `fact`

.

For example, if the user enters 5 as input, the `while`

loop multiplies `fact`

by each integer from 1 to 5 in turn, and then outputs the final value of `fact`

, which is 120. Therefore, the program outputs “Factorial of 5 is 120”.

```
#include <stdio.h>
int main() {
int num;
int fact = 1;
int i = 1;
printf("Enter a non-negative integer: ");
scanf("%d", &num);
while (i <= num) {
fact *= i;
i++;
}
printf("Factorial of %d is %d\n", num, fact);
return 0;
}
output
Enter a non-negative integer: 5
Factorial of 5 is 120
```

Overall, this post is a valuable resource for anyone seeking to write a **C program** for calculating the factorial of a number, and provides clear explanations and examples of each approach, making it easy for beginners to follow along.

Read More :- C PROGRAM TO CHECK WHETHER A GIVEN NUMBER IS A PALINDROME OR NOT

[…] The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding numbers, starting with 0 and 1. The first few numbers in the Fibonacci sequence are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, and so on. In this article, we will show you how to write a program in Algol that generates the first n numbers in the Fibonacci sequence. […]

[…] adaptability. In this article, we will show you how to implement the insertion sort algorithm in Algol to sort a list of […]

[…] Overall, quick sort is a highly efficient sorting algorithm that can handle even very large datasets with relative ease. While there are some considerations to keep in mind when implementing the algorithm, it remains a popular choice for sorting in many programming languages, including Algol. […]

[…] and removing noise. They can also be used to apply various filters and effects to images such as factorial program in c […]

[…] a Series Program in Java: A Beginner’s […]

Your article helped me a lot, is there any more related content? Thanks! https://www.binance.com/ru/register?ref=PORL8W0Z

Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me. https://accounts.binance.com/fr/register-person?ref=53551167

Thanks for sharing. I read many of your blog posts, cool, your blog is very good. https://accounts.binance.com/lv/register-person?ref=UM6SMJM3

[…] factorial program in C using functions is a common exercise for beginners learning the language. This program calculates the […]

The point of view of your article has taught me a lot, and I already know how to improve the paper on gate.oi, thank you. https://www.gate.io/de/signup/XwNAU

Your article gave me a lot of inspiration, I hope you can explain your point of view in more detail, because I have some doubts, thank you.

Your article gave me a lot of inspiration, I hope you can explain your point of view in more detail, because I have some doubts, thank you.

Your article gave me a lot of inspiration, I hope you can explain your point of view in more detail, because I have some doubts, thank you. 20bet

Great wordpress blog here.. It’s hard to find quality writing like yours these days. I really appreciate people like you! take care

Hey! Do you use Twitter? I’d like to follow you if that would be ok. I’m undoubtedly enjoying your blog and look forward to new posts.

Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me. https://www.binance.info/sl/join?ref=RQUR4BEO