• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C » Factorial Program in C
Next →
← Prev

Factorial Program in C

By Dinesh Thakur

Factorial Program in C: The factorial of a positive integer n, denoted by n!, is the product of all positive descending integers less than or equal to n: Syntax for factorial number is:

Factorial Program in C
n! = n.(n - 1).(n - 2).(n - 3)....3.2.1.

For example, the factorial of 5 (denoted as 5!) is

5! = 5.4.3.2.1 = 120 

The factorial of 0! is 1, according to the convention for an empty product.

The Factorial operation in many mathematical areas is used in permutations and combinations, algebra and mathematical analysis.

We’ll be covering the following topics in this tutorial:

  • Algorithm of factorial program in C
  • Pseudocode of factorial program in C
  • Factorial in C using a for loop
  • Factorial program in C using recursion

Algorithm of factorial program in C

START
 Step 1 → Enter the value of Fact.
 Step 2 → From value fact upto 1 multiply each digit.
 Step 4 → The final value is factorial Number.
STOP

Pseudocode of factorial program in C

procedure factorial(n)
  FOR value = 1 to n
   factorial = factorial * value
  END FOR
   DISPLAY factorial
end procedure

Factorial in C using a for loop

#include<stdio.h>
int main() {
  int i, Digit, fact = 1;
  printf("Enter a number to calculate its factorial : ");
  scanf("%d", &Digit);
  for (i = 1; i <= Digit; i++)
    fact = fact * i;
    printf("Factorial of %d = %d\n", Digit, fact);
    return 0;
}

Step by Step working of the above Program Code:

• First, the computer reads the number to find the factorial.
• Then, using the loop, the 'i' value is multiplied by the 'fact' value.
• The loop continues until the 'Digit' value.
• Finally, the given number's factorial value is printed.
Suppose the number the user enters is 5
• It assigns the value of Digit = 5 , i = 1 , fact = 1</span>
• Then the loop continues till the condition of the for loop is true.</span>
1. i<=Digit (1<=5) , for loop condition is true.
   fact = fact * i (fact = 1*1) So fact = 1
   i++ (i=i+1) So i = 2
2. i<=Digit (2<=5) , for loop condition is true.
   fact = fact * i (fact = 1*2) So fact = 2
   i++ (i = i+1) So i = 3
3. i<=Digit (3<=5) , for loop condition is true.
   fact = fact * i (fact = 2*3) So fact = 6
   i++ (i=i+1) So i=4
4. i<=Digit (4<=5) , for loop condition is true.
   fact = fact * i (fact = 6*4) So fact = 24
   i++ (i=i+1) So i = 5
5. i<=Digit (5<=5) , for loop condition is true.
   fact = fact * i (fact = 24*5) So fact = 120
   i++ (i=i+1) So i = 6
6. i<=Digit (6<=5) , for loop condition is true.
   It comes out of the for loop.
   Finally it prints as given below
   The Factorial of 5 is 120

Thus the program execution is completed.

Output:

Factorial in C using a for loopFactorial program in C using recursion

#include<stdio.h>
#include<conio.h>
void main() {
  int Digit,factorial;
  printf("\n Enter the number: ");
  scanf("%d",&Digit);
  factorial = fact(Digit);
  printf("\n The factorial of the number %d is %d",Digit,factorial);
}
int fact(int n) {
 if(n==0 || n==1)
   return 1;
 else
   return(n * fact(n-1));
 }

A function calls itself in recursion. The factorial function is called in the above program. It would help if you first expressed your solution in the recursive form to resolve a recursion problem.

Step by Step working of the above Program Code:

• Then, the factorial value is calculated using a recursive function and returns the factorial value to the main function.
• Finally, the given number’s factorial value is printed.

Let us assume that the number entered by the user is 5.

• It assigns the value of n = 5.
• Then, using a variable fact, the recursive function fact(n) is called.

1. n==0 || n==1 (5==0 || 5==1) if condition is false.
    So it goes to the else part.
  else
    return(n * fact(n-1)) So, (return(5 * fact(4))) 
2. n==0 || n==1 (4==0 || 4==1) if condition is false.
   So it goes to the else part.
 else
   return(n * fact(n-1)) So, (return(4 * fact(3)))
3. n==0 || n==1 (3==0 || 3==1) if condition is false.
   So it goes to the else part.
  else
   return(n * fact(n-1)) So, (return(3 * fact(2))) 
4. n==0 || n==1 (2==0 || 2==1) if condition is false.
   So it goes to the else part.
  else
   return(n * fact(n-1)) So, (return(2 * fact(1))) 
5. n==0 || n==1 (1==0 || 1==1) if condition is true.
   
   Finally, it prints as given below.
   The Factorial of the number 5 is 120
   Thus the program execution is completed.

Output:

Factorial program in C using recursion

You’ll also like:

  1. Factorial Program in Java
  2. Factorial Program in Python
  3. C Program Calculate Factorial of a Number using Recursion
  4. C Program Find the Factorial of N Number
  5. Write A C++ Program To Find The Factorial Of A Number By Using The Recursion.
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C Programming

C Programming Tutorials

  • C - History
  • C - Anatomy
  • C - Constants
  • C - Identifiers
  • C - Data Types
  • C - Libraries File
  • C - Header Files
  • C - Basic Language
  • C - Data Types Sizes
  • C - Header Files Importance
  • C - Escape Sequences
  • C - Main() Purpose
  • C - Program Procedure
  • C - Control Statements
  • C - Enumeration Constant
  • C - Add numbers
  • C - Return Statement
  • C - Avoid Goto
  • C - Command Line Arguments
  • C - Switch Case
  • C - Switch Case Limitations
  • C - getchar() and putchar()
  • C - Iteration Statements
  • C - Pass by Value and Reference
  • C - Structures and Unions
  • C - Structure
  • C - Dynamic Memory
  • C - Fgets and Fputs Functions
  • C - Gets() and Puts() Functions
  • C - Armstrong Number
  • C - Storage Classes
  • C - Fibonacci Series
  • C - Precision Setting
  • C - const Parameters

C - Variable & It's Type

  • C - Variables
  • C - Variable Lifetime
  • C - Static Variable
  • C - Register Variable
  • C - Global Variables
  • C - Auto Variables
  • C - Local Variables

C - Operator & Expressions

  • C - Operator
  • C - Boolean Operators
  • C - Bitwise Operator
  • C - Arithmetic Operators
  • C - Modulus Operator
  • C - Ternary Operator
  • C - Expressions
  • C - Arithmetic Expressions

C - Array

  • C - Arrays
  • C - Array Types
  • C - Array Characteristics
  • C - Static Arrays
  • C - Global Arrays
  • C - 3D Arrays
  • C - Dynamic Arrays
  • C - Pointer to 3D Arrays
  • C - Array Elements Hold
  • C - Arrays as Function Parameters
  • C - Accessing Matrix Elements
  • C - File Handling
  • C - Matrix Multiplication
  • C - Dynamic Memory Allocation

C - Searching & Sorting

  • C - Data Structures
  • C - Linear Search
  • C - Bubble Sort
  • C - Merge Sort
  • C - Linked List
  • C - Insertion Sort
  • C - Binary Search
  • C - Selection Sort
  • C - Quick Sort

C - Functions

  • C - Functions
  • C - Functions Advantages
  • C - Void Functions
  • C - Function Call
  • C - Default Return Value
  • C - String functions

C - Pointer

  • C - Pointers
  • C - Type Casting Of Pointers
  • C - Pointer Advantages
  • C - Pointers Initialization
  • C - Vectors and Pointers

C - Differences

  • C - C Vs C++
  • C - Formal Args. Vs Actual Args.
  • C - Keywords Vs Identifiers
  • C - Strings Vs Character Arrays
  • C - Address Vs Dereference Operator
  • C - Goto Vs longjmp
  • C - Declaring Vs Defining Variable
  • C - String Vs Array
  • C - Call by Value Vs Reference
  • C - Structure Vs Union
  • C - For Vs While loops
  • C - Compiler Vs Interpreter

C - Programs

  • C Program Standard Deviation
  • C Program Calculate Tax
  • C Program Sum Series
  • C Program Merge Arrays
  • C Program Euclid’s Algorithm
  • C Program Print Weekdays
  • C Program Sum of Digits
  • C Program Print a list
  • C Program Print Pythagorean
  • C Program Quiz program
  • C Program Display Table
  • C Program Print Comma-Separated
  • C Program Prints Prime Numbers
  • C Program for Print Integer
  • C Program Count Number
  • C Program Print Color Name
  • C Program Print Odd Numbers
  • C Program Calculate area
  • C Program for a Menu
  • C Program Add Two Vectors
  • C Program Array Addresses
  • C Program Division by Zero Error
  • C Program Compare two Dates
  • C Program Tower of Hanoi
  • C Program return 3 Numbers
  • C Program for Prime Numbers
  • C Program for Factorial
  • C Program for Palindrome

Other Links

  • C Programming - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW