Factorial Of A Number

Factorial of a Number

Factorial can be calculated simply using a C program. A factorial of a positive integer n is equal to 1 x 2 x 3 x n. It is often calculated using a loop. The following algorithm states the step-wise process to calculate factorial of a given number.

Algorithm to calculate Factorial of a Number

Step 1. Start

Step 2. Enter the number.

Step 3. Variable fact is initialized by 1

Step 3. For c < = n, fact is assigned the result by multiplying it with c

Step 4.  Factorial value is stored in fact.

Step 5. Factorial is printed.

Step 6. Stop

Read Also: Fibonacci Series Up to N

C Program to calculate Factorial of a Number

#include <stdio.h>
#include <conio.h>
int main()
{
  int c, n, fact = 1;
  
printf("Insert the number \n");
  
scanf("%d", &n);
  
//Implementing for loop to calculate factorial

for (c = 1; c <= n; c++)
    fact = fact * c;
  
printf("Factorial of %d = %d\n", n, fact);
  return 0;
}

Output

Enter the number: 6
Factorial: 720