Q. C Program to Find Factorial of a Given Number.

Here you will find an algorithm and program in C programming language to find factorial of a number. First let us understand what factorial means.

Explanation : The factorial of a number is the product of all the integers from 1 to that number.

For Example : Factorial of 5 is 120 i.e. 5*4*3*2*1

Algorithm to find factorial of a given number


START
step 1 : Declare/Read the number n
step 2 : [Initialize]
         i=1, fact=1
step 3 : Repeat step 4 and 5 until i=n
step 4 : fact=fact*i
step 5 : i=i+1
step 6 : Print fact
STOP

C Program to Find Factorial of a Number

#include <stdio.h>
int main() 
{
  int num = 7, i;
  unsigned long long fact = 1;
  if (num < 0)
    printf("Error! Factorial of a negative number doesn't exist.");
  else 
  {
    for (i = 1; i <= num; i++) {
      fact *= i;
    }
    printf("Factorial of %d = %llu", num, fact);
  }
  return 0;
}

Output

Factorial of 7 = 5040