- C Programming Examples
- C Programming Examples
- C Print Hello World
- C Get Input from User
- C Print Integer
- C Add Two Numbers
- C Add Subtract Multiply Divide
- C Add n Numbers
- C Area Perimeter of Square
- C Area Perimeter of Rectangle
- C Area Circum of Circle
- C Fahrenheit to Celsius
- C Celsius to Fahrenheit
- C Inches to Centimeters
- C Kilogram to Gram
- C Reverse a Number
- C Swap Two Numbers
- C Interchange Numbers
- C Print ASCII Value
- C Print Fibonacci Series
- C Check Palindrome or Not
- C Check Armstrong or Not
- C Find Armstrong Numbers
- C Find nCr and nPr
- C Find Profit Loss
- C Sum of their Square
- C First & Last Digit Sum
- C Sum of All Digit
- C Product of All Digit
- C Print Total Digit in Number
- C Check Perfect Number
- C Find Basic Gross Salary
- C Round Number to Integer
- C Print Series upto n Term
- C Find Factors of Number
- C if-else & Loop Programs
- C Check Even or Odd
- C Check Prime or Not
- C Check Alphabet or Not
- C Check Vowel or Not
- C Check Leap Year or Not
- C Is Reverse Equal Original
- C Make Calculator
- C Add Digits of Number
- Count Positive Negative Zero
- C Largest of Two Numbers
- C Largest of Three Numbers
- C Smallest of Two Numbers
- C Smallest of Three Numbers
- C Find Factorial of Number
- C Find LCM & HCF
- C Find LCM of n Numbers
- C Find HCF of n Numbers
- C Find Arithmetic Mean
- C Find Average, Percentage
- C Find Student Grade
- C Print Table of Number
- C Print Prime Numbers
- C Find Discount Purchase
- C Calculate Parcel Charge
- C Calculate Wage of Labor
- C Print Phone Bill
- C Conversion programs
- C Decimal to Binary
- C Decimal to Octal
- C Decimal to Hexadecimal
- C Binary to Decimal
- C Binary to Octal
- C Binary to Hexadecimal
- C Octal to Decimal
- C Octal to Binary
- C Octal to Hexadecimal
- C Hexadecimal to Decimal
- C Hexadecimal to Binary
- C Hexadecimal to Octal
- C Pattern Programs
- C Pattern Printing Programs
- C Print Diamond Pattern
- C Print Floyd's Triangle
- C Print Pascal's Triangle
- C Array Programs
- C 1D Array Programs
- C Linear Search
- C Binary Search
- C Largest Element in Array
- C Smallest Element in Array
- C Second Largest/Smallest
- C Count Even Odd
- C Array Element at Even
- C Array Element at Odd
- C Print Even Array Elements
- C Print Odd Array Elements
- C Sum/Product of Even/Odd
- C Reverse an Array
- C Insert Element in Array
- C Delete Element from Array
- C Merge Two Arrays
- C Bubble Sort
- C Selection Sort
- C Insertion Sort
- C Print Common Elements
- C 2D Array Programs
- C Add Two Matrices
- C Subtract Two Matrices
- C Transpose a Matrix
- C Multiply Two Matrices
- C Sum All Matrix Elements
- C Largest Element in Matrix
- C Print Row Column Total
- C 3D Array Programs
- C String Programs
- C Print String
- C Find Length of String
- C Compare Two String
- C Copy a String
- C Concatenate String
- C Reverse a String
- C Count Vowels Consonants
- C Replace Vowel in String
- C Delete Vowels from String
- C Delete Word from String
- C Frequency of Character
- C Count Word in String
- C Remove Spaces from String
- C Sort a String
- C Sort String in Alphabetical
- C Sort Words in Ascending
- C Sort Words in Descending
- C Uppercase to Lowercase
- C Lowercase to Uppercase
- C Swap Two Strings
- C Check Anagram or Not
- C Check Palindrome String
- C Print Number in Words
- C Print Successive Character
- C Character without Space
- C Remove Extra Spaces
- C File Programs
- C Read a File
- C Write Content to File
- C Read & Display File
- C Copy a File
- C Merge Two Files
- C Reverse File
- C Count All Character in File
- C List Files in Directory
- C Encrypt & Decrypt a File
- C Delete a File
- C Misc Programs
- Generate Random Numbers
- C Print Date Time
- C Print Message with Time
- C Get IP Address
- C Print Smiling face
- C Pass Array to Function
- Add Two Numbers using Pointer
- C Address of Variable
- C Shutdown Computer
- C Programming Tutorial
- C Tutorial
- C Programming Library
- C Standard Library
- C Programming Test
- C Programming Test
- Give Online Test
- All Test List

# C Program to Find Factorial of Number

In this tutorial, we will learn about how to find and print factorial of any given number with and without using function. Here we have also created a recursive function to find out the factorial of any number. Here are the list of programs you will go through:

- Find Factorial of Given Number
- Find Factorial of Given Number using Function
- Find Factorial of Given Number using Recursion
- Find Factorial of all Numbers in Given Range

But before starting these programs, let's understand how factorial of a number gets calculated.

### How to Find Factorial of any Number ?

To find factorial **f** of any number say **n**, use the formula

Factorial of n (n!) = (n)*(n-1)*(n-2)*....*3*2*1

Or

n! = 1*2*3*....*(n-2)*(n-1)*n

For example, find factorial of **5**, using the above formula

5! = 5*4*3*2*1 = 120

So factorial of 5 is 120. Now let's move on and implement it in a C program.

## Find Factorial of Given Number in C

To find the factorial of any given number in C programming, you have to ask from user to enter the number. Now find the factorial of
that number using above formula as shown in the program given below. As you can see from the formula, if a number is **n**, then
multiplication gets occurred for **n** times.

Or for example, if 5 is the number, then multiplication gets occurred 5 times, that is **5*4*3*2*1**. So here it is required
to create loop that runs **n** number of times

#include<stdio.h> #include<conio.h> int main() { int num, i, fact=1; printf("Enter any number: "); scanf("%d", &num); for(i=num; i>0; i--) fact = fact*i; printf("\nFactorial of %d = %d", num, fact); getch(); return 0; }

As the program was written under **Code::Blocks** IDE, therefore after successful build and run you will get the following output:

Now supply any number say **5** and press ENTER key to see the factorial of 5 as output as shown here:

#### Program Explained

- Get the number as input say
**5**using**scanf()**function - Create a for loop starting from the number say 5 in this case
- Continue running the for loop until the number (here variable i holds the value of num) becomes 0
- If user has provided the number
**5**as input, then at first run of the for loop, i holds 5 which is greater than 0, and here fact holds 1 as its initial value, therefore**fact*i**or**5*1**or**5**is initialized to**fact**variable - At second run
**i**is decremented and holds the value**4**which is again greater than 0, therefore inside the loop,**fact*i**or**5*4**or**20**is initialized to fact. In same way, at third, fourth, and fifth run, fact holds its final value as**5*4*3*2*1**or**120**. At sixth time, when i gets decremented and becomes 0 which is not greater than 0, therefore the condition evaluates to false, therefore the program flow goes out of the**for**loop - Now print the value of
**fact**variable, that contains the factorial value say 120 of given number say 5

## Find Factorial of a Number using Function

This program does the same job, but using user-defined function **findFact()**. To learn about function, then
refer to its separate tutorial where you can go for its in-depth tutorial.

#include<stdio.h> #include<conio.h> int findFact(int); int main() { int num, fact; printf("Enter any number: "); scanf("%d", &num); fact = findFact(num); printf("\nFactorial of %d = %d", num, fact); getch(); return 0; } int findFact(int n) { int i=n, f=1; while(i>0) { f = f*i; i--; } return f; }

## Find Factorial of a Number using Recursion

Here is another program that will calculate the factorial of any given number using recursive function:

#include<stdio.h> #include<conio.h> int findFact(int); int main() { int num, fact; printf("Enter the number: "); scanf("%d", &num); fact = findFact(num); printf("Factorial = %d", fact); getch(); return 0; } int findFact(int val) { if(val==1) return val; else return val*findFact(val-1); }

Here is the final snapshot of the sample run:

#### Program Explained

- Declare the function with one argument to pass the given value or number by user
- Inside the main function, receive number as input and call the function in such a way the function will initialize its
returning value to the variable that contains the factorial value of given number say
**fact** - In recursive function definition, we have checked whether the value or number (passed through argument) is equal to 1 or not,
if it is, then return the value of variable
**val**, otherwise return**val*findFact(val-1)** - For example, if user has supplied 5 as input, then 5 is the argument that gets passed to the recursive function, at first time,
5 is not equal to 1, therefore the program flow goes to the
**else**block's statement, here the returning value is**5*findFact(5-1)**or**5*findFact(4)**. As we have return the function itself, therefore the function**findFact()**gets called itself (that is called recursive function). - The function is called with argument
**4**, therefore again 4 is not equal to 1, then again the program flow goes to the**else**block's statement, therefore the return value will be this time, return**5*4*findFact(4-3)**or**20*findFact(3)** - In same way the final return value before the argument value becomes 1 will be return
**120*findFact(2-1)**or**120*findFact(1)** - At last when the argument value is 1, then the program flow goes to the
**if**block's statement, that return only the value of variable**val**that holds the factorial value say 120 of the number that has passed at first time as argument to the function say 5 here - Finally 120 is initialized to the variable
**fact**inside the**main()**function - Print the value of
**fact**variable, that contains the factorial value of the given number

If you are not satisfied with above explanation. Then I recommend you to refer Recursion in C tutorial. There you will of course understand all about it, that how recursion works.

## Find Factorial of all Numbers in Given Range

Now this is the last program that will find factorial of all numbers in given range. For example, if user enters **1** and **10**
as two number (the range). Then the program will find and prints factorial of all numbers from 1 to 10.

#include<stdio.h> #include<conio.h> int findFact(int); int main() { int num1, num2, i; long int fact; printf("Enter Range: "); scanf("%d %d", &num1, &num2); if(num1<num2) { for(i=num1; i<=num2; i++) { fact = findFact(i); printf("\nFactorial of %d = %ld", i, fact); } } else { for(i=num2; i<=num1; i++) { fact = findFact(i); printf("\nFactorial of %d = %ld", i, fact); } } getch(); return 0; } int findFact(int n) { int i=n; long int f=1; while(i>0) { f = f*i; i--; } return f; }

Here is its sample run:

#### Same Program in Other Languages

« Previous Program Next Program »