Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is the Factorial Program in C?
3.
Algorithm of Factorial Program in C
4.
Factorial Program in C using For Loop
4.1.
C
5.
Factorial Program in C using Recursion
5.1.
C
5.2.
Using Tail Recursion
5.3.
C
6.
Factorial Program in C using Ternary Operator
6.1.
C
7.
Factorial Program using tgamma() Method
7.1.
C
8.
Factorial Program using Pointers
8.1.
C
9.
Tips for Writing Efficient Factorial Programs
10.
Frequently Asked Questions
10.1.
How to write factorial formula in C?
10.2.
What is an example of a factorial number in C?
10.3.
What is the factorial of 100 factorial?
11.
Conclusion
Last Updated: Aug 11, 2024
Easy

Factorial Program in C

Author Rashmi
0 upvote

Introduction

Factorial is a mathematical function that computes the product of all positive integers less than or equal to a given number. This article will explore how to create a factorial program in C, along with frequently asked questions related to the topic.

Factorial Program in C

What is the Factorial Program in C?

The factorial of a non-negative integer n is denoted as n! and is defined as the product of all positive integers less than or equal to n. The factorial function can be formally defined as:

n! = n * (n-1) * (n-2) * ... * 1 for n > 0

0! = 1

A C program that computes the factorial of a given number is known as a factorial programme. In C, there are two primary approaches to writing a factorial programme:

Using a loop: This method multiplies each positive integer by the factorial variable by iterating through all positive integers less than or equal to the supplied value using a for loop.

Recursion: This technique uses this technique to determine the factorial of the input integer. The recursive function multiplies the result by the input number after computing the factorial of the number minus 1.

Algorithm of Factorial Program in C

  1. Start: Begin the program execution.
  2. Input: Read the integer n for which the factorial needs to be calculated.
  3. Initialize: Set a variable factorial to 1. This will hold the result of the factorial calculation.
  4. Loop:
    1. Use a loop to multiply factorial by each integer from 1 to n.
    2. Update each iteration's factorial with the current value's product and the loop counter.
  5. Output: Print the value of the factorial after the loop completes.
  6. End: Terminate the program execution.

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

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

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

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

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

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

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:

  1. Decimal to Binary in C
  2. Ternary Operator in C
  3. Conditional Operator in C
  4. Prime Number Program using C

 

Happy Learning!

Live masterclass