Factorial Program in C using For Loop
An iterative approach to finding the factorial of a number involves using a for loop to multiply the numbers from 1 to the given number. Here's the code:
C
#include <stdio.h>
unsigned long long factorial(unsigned int n) {
unsigned long long result = 1;
for (unsigned int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
int main() {
unsigned int n;
printf("Enter a number: ");
scanf("%u", &n);
printf("Factorial of %u is %llu\n", n, factorial(n));
return 0;
}
You can also try this code with Online C Compiler
Run Code
Factorial Program in C using Recursion
A recursive approach to finding the factorial involves calling the function itself with a reduced problem size. Here's the code:
C
#include <stdio.h>
unsigned long long factorial(unsigned int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
unsigned int n;
printf("Enter a number: ");
scanf("%u", &n);
printf("Factorial of %u is %llu\n", n, factorial(n));
return 0;
}
You can also try this code with Online C Compiler
Run Code
Using Tail Recursion
Tail recursion optimizes recursive functions by reducing the amount of memory used. Here's the factorial code with tail recursion:
C
#include <stdio.h>
unsigned long long factorial_helper(unsigned int n, unsigned long long acc) {
if (n == 0) {
return acc;
} else {
return factorial_helper(n - 1, acc * n);
}
}
unsigned long long factorial(unsigned int n) {
return factorial_helper(n, 1);
}
int main() {
unsigned int n;
printf("Enter a number: ");
scanf("%u", &n);
printf("Factorial of %u is %llu\n", n, factorial(n));
return 0;
}
You can also try this code with Online C Compiler
Run Code
Factorial Program in C using Ternary Operator
The ternary operator can be used in C to compute factorials, but it's crucial to remember that while it can result in concise code, it might not be the most readable or effective method. Here is a ternary operator factorial program:
C
#include <stdio.h>
// Function to calculate factorial using ternary operator
unsigned long long factorial(int n) {
return (n == 0) ? 1 : n * factorial(n - 1);
}
int main() {
int m;
printf("Enter your non-negative integer: ");
scanf("%d", &m);
if (m < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
unsigned long long res = factorial(m);
printf("Factorial of %d is %llu\n", m, res);
}
return 0;
}
You can also try this code with Online C Compiler
Run Code
Factorial Program using tgamma() Method
The tgamma() function from the C++ standard library calculates the Gamma function, which is closely related to the factorial function. For a positive integer n, tgamma(n) returns the factorial of n-1. Therefore, to get the factorial of n, use tgamma(n + 1).
Example:
C
#include <iostream>
#include <cmath> // For tgamma()
int main() {
int n;
std::cout << "Enter a positive integer: ";
std::cin >> n;
// Calculate factorial using tgamma()
double factorial = tgamma(n + 1);
std::cout << "Factorial of " << n << " is " << factorial << std::endl;
return 0;
}
You can also try this code with Online C Compiler
Run Code
Output:
Enter a positive integer: 5
Factorial of 5 is 120
Factorial Program using Pointers
Calculating the factorial using pointers involves creating a function that calculates the factorial and using a pointer to pass the result back to the caller.
Example:
C
#include <iostream>
// Function to calculate factorial using pointers
void calculateFactorial(int n, long long *result) {
*result = 1;
for (int i = 1; i <= n; i++) {
*result *= i;
}
}
int main() {
int n;
long long factorial;
std::cout << "Enter a positive integer: ";
std::cin >> n;
// Calculate factorial using pointers
calculateFactorial(n, &factorial);
std::cout << "Factorial of " << n << " is " << factorial << std::endl;
return 0;
}
You can also try this code with Online C Compiler
Run Code
Output:
Enter a positive integer: 5
Factorial of 5 is 120
Tips for Writing Efficient Factorial Programs
Tips for writing efficient factorial programs are:-
- Choose the right approach: Based on your specific use case and the size of the input numbers, select the most appropriate approach for your factorial program.
- Optimize for large numbers: If your program needs to handle very large numbers, consider using an array-based approach to store and compute the factorial.
- Error handling: Implement proper error handling for edge cases, such as negative numbers or numbers that exceed the maximum representable value of the chosen data type.
Also read reverse a number.
Frequently Asked Questions
How to write factorial formula in C?
Apply a loop or recursion to create a factorial formula in C. An example of a recursive method is factorial(n) = (n == 0) ? 1 : n * factorial(n - 1). For instance, 120 is the factorial of 5 (5!).
What is an example of a factorial number in C?
By multiplying all the positive integers that are less than or equal to the given number, a factorial number is a number that can be calculated in C. For instance, since 5 is the sum of 1, 2, 3, 4, and 5, its factorial is 120.
What is the factorial of 100 factorial?
The sum of all positive integers less than or equal to 100 factorial is known as the factorial of 100 factorial. This number is roughly equal to 9.3326215443944e+157, although it cannot be printed out in full due to its size.
Conclusion
In this article, we have discussed factorial program in C. Calculating the factorial of a number is a fundamental concept in programming that serves as a great introduction to iterative and recursive techniques. In C, you can compute factorials using simple loops or more advanced methods, such as leveraging the tgamma() function from the C++ standard library for efficient computation.
Recommended Reading:
- Decimal to Binary in C
- Ternary Operator in C
- Conditional Operator in C
- Prime Number Program using C
Happy Learning!