Write a program in C to find the factorial of a given number

Write a program in C language to find the factorial of a given number. In this article, we will create a program in C to find the factorial of a given number. As factorial is a major part of mathematics in permutation and combination, these concepts are used in finding the probabilities and combination of problems and their solutions.

Introduction

Factorial: The factorial is a simple concept of multiplication of the number likewise, The factorial of the 5 is 120 which is calculated as 5 x 4 x 3 x 2 x 1=120. Similarly, the factorial of 6 is 720 (6 x 5 x 4 x 3 x 2 x 1=720).

Algorithm

  • Start
  • Declare variables.
  • Using for loop to calculate the factorial.
  • Now print the value obtained.
  • End

C Program to Find Factorial of a Number

//factorial of a number
#include<stdio.h>
void main(){
  int i=1;
  int fact;
  int a=5;
  for(i;i<=5;i++){
    fact=fact*i;
  }
  printf("the factorial of %d is %d",a,fact);
}

Explanation of the Code

  • On-Line no:2 we include the header files to take input and output.
  • On-Line no:3 we definite the main() method.
  • On-Line no:4, 5, 6 we have initialized all the variables that we are going to use further.
  • On-Line no:7 we have used for loop to make the logical expression for multiplication.
  • On-Line 8, updating the factorial(fact) again and again till the limit reaches
  • On-Line 10, printing the suitable messages to show the factorial.

Output

In this way, we have created C Program to find the factorial of a number.