Program of Factorial in C with Example code & output | DataTrained

Chandrakishor Gupta Avatar

Introduction

The sum of the multiplications of all the integers smaller than a positive integer results in the factororial of that positive integer. program of factorial in c, The factorial of 5, for instance, is 120, which is equal to 5 * 4 * 3 * 2 * 1. Program of Factorial in C: To find the factor of n, put up all positive descending integers.

When trying to determine how many distinct ways there are to mix things or arrange them, it is quite helpful. For the initial decision, we have n options.

What is the Program of Factorial in C?

What is the Program of Factorial in C?

This program uses a for loop to compute the factorial after receiving a positive number from the user. The factorial variable’s type is unsigned long long because a number’s factorial could potentially be very huge. The software displays a unique error message if the user enters a negative number.

The factor of n is obtained by adding all positive descending integers. N! is hence referred to as a Factorial of n.

An example of a factorial is “!”. Therefore, if you wish to get the factorial of the integer n, you can do it by using the formula n! = n*(n-1)*(n-2)*(n-3)… . When multiplying the integers from 1 to n, the result is the factorial of n, which is indicated by the symbol n!. n! = n (n – 1)! is the formula for n factorial.

Algorithm of Program of Factorial in C

A C programme uses the following algorithm to determine a number’s factorial:

  • Launch the programme
  • Asking the user for an integer will help you determine the factorial.
  • After reading the integer, assign it to a variable.
  • By increasing each digit from the integer’s value up to 1, the ultimate value is updated.
  • The factorial is the final result after all multiplications up to one.
  • pause the programme

Find The Program of Factorial in C

Find The Program of Factorial in C

There are two techniques to calculate the factorial of numbers.

  1. Factorial Program using Iterative Solution
  • Using For Loop
  • Using While loop 
  1. Factorial Program using Recursive Solution
  • Using Recursion
  • Using Ternary Operator

Factorial Program using Iterative Solution

Factorial Program using Iterative Solution

Since recursion can be expensive for big numbers, factororial can alternatively be calculated iteratively. Program of Factorial in C, Here, we’ve used both for and while loops to demonstrate the iterative technique.

Program of Factorial in C Using For Loop 

In order to calculate the factorial of an integer, we will first create a C programme using a for loop. Program of Factorial in C, There will be an integer variable in the programme with the value 1. Till the value equals the value the user entered, the for loop will keep raising the value by 1 with each iteration. The user-inputted number’s factorial will be the final value in the fact variable.

Code-

// C program to implement

// the above approach

#include <stdio.h>

// Function to find factorial of

// given number

unsigned int factorial(unsigned int n)

{

int res = 1, i;

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

res *= i;

return res;

}

// Driver code

int main()

{

int num = 5;

printf(“Factorial of %d is %d”,

num, factorial(num));

return 0;

}

Output:

Factorial of 5 is 120

Program of Factorial in C Using While loop  

Using a while loop, we will put the technique into practise and discover the program of factorial in C.

Code-

// C program for factorial of

// a number

#include <stdio.h>

// Function to find factorial

// of given number

unsigned int factorial(unsigned int n)

{

if (n == 0)

return 1;

int i = n, fact = 1;

while (n / i != n) {

fact = fact * i;

i–;

}

return fact;

}

// Driver code

int main()

{

int num = 5;

printf(“Factorial of %d is %d”,

num, factorial(num));

return 0;

}

Output

Factorial of 5 is 120

Click here to know about: data science institute in Delhi

Factorial Program using Recursive Solution

Factorial Program using Recursive Solution

The following recursive formula can be used to determine the program of factorial in C.

 n! = n * (n-1)!

  When n = 0 or 1, n! = 1.

Factorial Program Using Recursion in C

Now, using a recursive function, we will create a program of factorial in C. Up till the value is not equal to 0, the recursive function will keep calling itself.

We will now create a C programme in which a recursive function will calculate factorial. Up till the value is not equal to 0, Program of Factorial in C, the recursive function will call itself.

Code-

// C program to find factorial

// of given number

#include <stdio.h>

// Function to find factorial

// of given number

unsigned int factorial(unsigned int n)

{

if (n == 0)

return 1;

return n * factorial(n – 1);

}

// Driver code

int main()

{

int num = 5;

printf(“Factorial of %d is %d”,

num, factorial(num));

return 0;

}

Output:

Factorial of 5 is 120

Factorial Program Using Ternary Operator in C

Similar to a shortcut for an if…else expression is the ternary operator. It offers two conditions and the declarations that must be carried out in accordance with those conditions. Here is a ternary operator-based the program of factorial in C written.

Here is a ternary operator-based factorial programme.

Code-

// C program to find factorial

// of given number

#include <stdio.h>

int factorial(int n)

{

// Single line to find factorial

return ((n == 1 || n == 0) ? 1 : n * factorial(n – 1));

}

// Driver Code

int main()

{

int num = 5;

printf(“Factorial of %d is %d”,

num, factorial(num));

return 0;

}

Also read: data science training in Chennai

C Program to Find Factorial Using Functi
on

To determine the factorial of a given number, you can alternatively develop your own function. Program of Factorial in C, The usage of functions to find factorial is demonstrated in the code below.

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:

Enter a number to get factorial: 8

The factorial of 8 is: 40320

Conclusion

You have studied many approaches to writing the Program of Factorial in C from this post. One of the fundamental C programmes that serves as an introduction to a number of ideas, including loops, operators, and functions, is the factorial programme. The programmes may now be used to calculate the factorial of any number.

Frequently Asked Questions

How do you write a factorial?

The factorial of a number (n!) is identical to n(n-1) in more mathematical words. For instance, you would write the following if you wanted to find the factorial for four: 5! = 5*4*3*2*1 = 120

A number’s factorial is the sum of all positive integer products that are less than the specified number. Factorial is one of the operations in mathematics and is symbolised by the symbol “!”. For instance, 8! (also known as 8 factorial) equals 8 7 6 5 4 3 2 1 = 40,320.

When examining permutations and combinations, we employ factorials. If the order of the components is important, permutations tell us how many distinct arrangements are possible. Combinations explains how many different ways there are to select k things from n items if their order is irrelevant.

The sum of all positive integers that are less than or equal to a specific positive integer, followed by an exclamation point, is referred to as the “factorial” in mathematics. Therefore, the symbol 7!, which stands for 1 2 3 4 5 6 7, is used to represent factorial seven.Factorial 0 is equivalent to 1 by definition.

In mathematics, a number’s factororial is the sum of all positive numbers that are less than or equal to the given number. The data set cannot be ordered since there are no positive values below zero, which qualifies as one of the many combinations of how data can be arranged (it cannot). Thus, 0! = 1.

Tagged in :

More Articles & Posts

UNLOCK THE PATH TO SUCCESS

We will help you achieve your goal. Just fill in your details, and we'll reach out to provide guidance and support.