Factorial Program In C



Factorial of a positive integer n is product of all values from n to 1. For example, the factorial of 3 is (3 * 2 * 1 = 6).

Algorithm

Algorithm of this program is very easy −

START
   Step 1 → Take integer variable A
   Step 2 → Assign value to the variable
   Step 3 → From value A upto 1 multiply each digit and store
   Step 4 → the final stored value is factorial of A
STOP

Pseudocode

We can draft a pseudocode of the above algorithm as follows −

procedure find_factorial(number)
   
   FOR value = 1 to number
      factorial = factorial * value
   END FOR
   DISPLAY factorial

end procedure

Implementation

Implementation of this algorithm is given below −

#include <stdio.h>

int main() {
   int loop;
   int factorial=1;
   int number = 5;

   for(loop = 1; loop<= number; loop++) {
      factorial = factorial * loop;
   }

   printf("Factorial of %d = %d \n", number, factorial);

   return 0;
}

Output

Output of the program should be −

Factorial of 5 = 120
mathematical_programs_in_c.htm
Advertisements