C Program For Factorial Of A Number Using For Loop

C Program For Factorial Of A Number Using For Loop.  If you are looking for a factorial program in C with an example, this factorial tutorial will help you to learn how to find the factorial of a number in C language. Just go through this C programming example for factorial, you will be able to write a C program for factorial using for loop.

C Program For Factorial – Table of Contents

What is Factorial?

Factorial is the product of a positive integer (n) and all the integers below of it (n-1). Example, the factorial of positive integer n is ( n! ) is equal to 1*2*3*…*n.

Example: What is the value of 4!

4! = 1*2*3*4 => 24

The value of 4! is 24.

C Program For Factorial

Learn how to write a  C program for factorial. Writing a C program to find factorial can be done using various techniques like using for loop, while loop, pointers, recursion but here in this program, we show how to write a factorial program using for loop in a proper way.

This is the eleventh C programming example in the series, it helps newbies coders, programming students and B.Tech graduates in enhancing their C programming skills and land on their dream job in software industry. All the best guys in learning c programs with coding compiler website. We wish all the success in your career. Happy Learning.

C Programming Tutorials

  1. C Program For Palindrome String
  2. C Program For Palindrome Numbers
  3. C Program To Reverse a String with Using Function
  4. C Program To Reverse a String without Using Function
  5. C Program To Reverse a String Using Recursion

C Program For Factorial Source Code

Copy the below C program to find the factorial of a number source code or write your own logic by using this program as a reference. Paste the factorial program into C compilers and run the program to see the result.

/* C PROGRAM FOR FACTORIAL - FACTORIAL.C */

#include <stdio.h>
int main()
{
 int n, i,factorial = 1; //varialbes declaration

printf("Enter a number: "); //asking your to enter a number
 scanf("%d",&n); //reading entered number

// showing error message, if the entered number is a negative number
 if (n < 0)
 printf("Error! Factorial of a negative number doesn't exist.");

else
 {
   for(i=1; i<=n; ++i) //finding factorial using for loop
   {
      factorial=factorial*i; // calculating factorial of a number
   }
   printf("Factorial of %d = %d", n, factorial);
 }

return 0;
}

C Program For Factorial Output

After you compile and run the above c program for factorial of a number using for loop, your C compiler asks you to enter a number to find factorial. After you enter your number, the program will be executed and give output like below expected output.

Output – 1

Enter a number: 4
Factorial of 4 = 24

Output – 2

Enter a number: -4
Error! Factorial of a negative number doesn’t exist.

C Programming Examples

  1. C Program To Reverse a String Using Pointers
  2. C Program To Swap Two Numbers Using Two Variables
  3. C Program To Swap Two Numbers Using Three Variables
  4. C Program For Prime Numbers – Check  a Number is Prime or Not
  5. C Program To Reverse a String with Using Function
  6. C Program to Reverse a String without Using Function
  7. C Program to Reverse a Sting Using Recursion

Leave a Comment