Factorial Program in C

Flow Control
If-else Statement in C
Programs on if-else
Switch Case in C
Switch case Programs
Conditional Operator
While loop in C
Do-while loop in C
While vs do-while
For loop in C
Break keyword in C
Continue keyword in C
Break vs Exit in C
Goto keyword in C
☕️ Flow Control Programs
Largest in 3 Numbers
Find Grade of student
Find the absolute value
Vowel or Consonant
Leap Year Program
Simple calculator in C
Check Odd or Even 
Roots of Quadratic Equation
Find Reverse of Number
Factors of a number in C
Generate Multiplication table
Find Power of a Number
Find GCD and LCM
Find factorial of Number
Count Number of Digits
Sum of digits in Number
Sum of N Natural Numbers
Sum of Squares of Natural No.
Find Sum of Odd Numbers
Find the Sum of Series
Find Fibonacci series in C
Sum of the Fibonacci series
Sum until enters +ve numbers
Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
Celsius to Fahrenheit
Fahrenheit to Celsius
Decimal ↔ Binary
Decimal ↔ Octal
Octal ↔ Binary in C
☕️ Number Programs in C
Prime Number in C
Strong Number in C
Krishnamurthy Number
Neon Number in C
Palindrome number
Perfect Number in C
Armstrong Number
☕️ Pattern Programs in C
Pattern programs in C
Printing pattern using loops
Floyd’s triangle Program
Pascal Triangle Program
Pyramid Star Pattern in C
Diamond Pattern Programs
Half Diamond pattern in C
Print Diamond Pattern
Hollow Diamond Pattern
Diamond Pattern of Numbers

In mathematics, the factorial of a positive integer n, denoted by n! It is the product of all positive integers less than or equal to n. For example:- The factorial of 4= 4! = 4*3*2*1 or 1*2*3*4 = 24 Here we will write the Factorial program in C programming language.

Factorial (n) = 1 * 2 * …. * (n-1) * n
Or,
Factorial (n) = n * (n-1) * ….. * 2 * 1

The factorial of a negative number doesn’t exist. The value of 0! is 1, according to the convention for an empty product.

Factorial of a Number in C using For loop

The for loop in C programming language is a pre-test loop, where first of all initialization expression is evaluated then the condition is checked and if the condition is true then only the statements of the for loop are executed.

The for loop contains the initialization, update, and test_condition in one statement, so it makes for very readable code. All the control steps, initialization, end-of-loop test, and updating are done in one place.

We can use the increment operator to find the factorial of a number. So, using increment operator the 4! is evaluated as 1*2*3*4

Factorial program using for loop and increment (++) operator

 #include<stdio.h>
 int main()
 {
     int number, i, fact=1;

     printf("Enter number: ");
     scanf("%d", &number);

     for(i=1; i<=number; i++)
     {
         fact *= i; // fact = fact * i
     }

     printf("%d! = %d", number, fact);

     return 0;
 }

Output:-

Enter number: 5
5! = 120

Factorial of a Number Using for loop and decrement (--) operator

We can use the decrement operator to find the factorial of a number. So, using the decrement operator for 4! = 4*3*2*1

 #include<stdio.h>
 int main()
 {
     int number, i, fact=1;

     printf("Enter number:");
     scanf("%d",&number);

     for(i=number; i>0; i--)
     {
         fact *= i; // fact = fact * i
     }

     printf("%d! = %d", number, fact);

     return 0;
 }

In this case, initialize the variable i with the number and use decrement operation as update expression until the number is greater than zero.

Factorial program in C using while loop

We can also write the factorial program in C using while loop. While loop in C is a pre-test loop where the expression is evaluated then only statements are executed. It uses a test expression to control the loop. Before every iteration of the loop, the test expression is evaluated.

 #include<stdio.h>
 int main()
 {
     int number, i=1, fact=1;

     printf("Enter number:");
     scanf("%d",&number);

     while(i<=number)
     {
       fact *= i;
       i++;
     }

     printf("%d! = %d", number, fact);

     return 0;
 }

Using the do-while loop

The do-while loop in C is very closely related to the while loop. The do keyword is placed on a line of code at the top of the loop. A block of statements follows it with a test expression after the keyword while, at the bottom of the loop.

It is a post-test loop. The post-test loop executes at least, only then control expression is tested. If the expression is true, the loop repeats. When the expression is false then the loop terminates.

We can also use the do-while loop to write the factorial program in C.

 #include<stdio.h>
 int main()
 {
     int number, i=1, fact=1;

     printf("Enter number: ");
     scanf("%d",&number);

     do
     {
       fact *= i;
       i++;
     } while(i<=number);

     printf("%d! = %d", number, fact);

     return 0;
 }

See also:- Factorial program in C using a function, Factorial program in C using Recursion

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Flow Control
If-else Statement in C
Programs on if-else
Switch Case in C
Switch case Programs
Conditional Operator
While loop in C
Do-while loop in C
While vs do-while
For loop in C
Break keyword in C
Continue keyword in C
Break vs Exit in C
Goto keyword in C
☕️ Flow Control Programs
Largest among 3 Numbers
Find Grade of student
Find the absolute value
Check Vowel or Consonant
Leap Year Program
Simple calculator in C
Check Odd or Even 
Roots of Quadratic Equation
Find Reverse of Number
Factors of a number in C
Generate Multiplication table
Find Power of a Number
Find GCD and LCM
Find factorial of Number
Count Number of Digits
Sum of digits in Number
Sum of N Natural Numbers
Sum of Squares of Natural No.
Find Sum of Odd Numbers
Find the Sum of Series
Find Fibonacci series in C
Sum of the Fibonacci series
Sum until enters +ve numbers
Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
Celsius to Fahrenheit
Fahrenheit to Celsius
Decimal ↔ Binary
Decimal ↔ Octal
Octal ↔ Binary in C
☕️ Number Programs in C
Prime Number in C
Strong Number in C
Krishnamurthy Number
Neon Number in C
Palindrome number
Perfect Number in C
Armstrong Number
☕️ Pattern Programs in C
Pattern programs in C
Printing pattern using loops
Floyd’s triangle Program
Pascal Triangle Program
Pyramid Star Pattern in C
Diamond Pattern Programs
Half Diamond pattern in C
Print Diamond Pattern
Hollow Diamond Pattern
Diamond Pattern of Numbers

1 thought on “Factorial Program in C”

Leave a Comment

Your email address will not be published. Required fields are marked *