
- Learn C By Examples Time
- Learn C by Examples - Home
- C Examples - Simple Programs
- C Examples - Loops/Iterations
- C Examples - Patterns
- C Examples - Arrays
- C Examples - Strings
- C Examples - Mathematics
- C Examples - Linked List
- C Programming Useful Resources
- Learn C By Examples - Quick Guide
- Learn C By Examples - Resources
- Learn C By Examples - Discussion
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