Write a C program to enter any number and calculate its factorial using for loop. How to find factorial of any number in C programming using loop. C program to find and display factorial of any given number.

Input number: 5

Output factorial: 120

For example factorial of 5 = 1 * 2 * 3 * 4 * 5 = 120

Happy coding ;)

**Example:**Input number: 5

Output factorial: 120

### Required knowledge

Basic C programming, For loop#### What is factorial

Factorial of any number is the product of all positive numbers less than or equal to n. It is denoted by n!.For example factorial of 5 = 1 * 2 * 3 * 4 * 5 = 120

### Logic to find factorial

Finding factorial is really easy you just need to know how to iterate over a loop. Finding factorial can be basically divided two steps:- Run a loop from 1 to n (where n is the number whose factorial is to be found).
- Multiply the current loop counter value with fact (where fact is a variable that stores factorial and is initially initialized with 1).

### Program to find factorial

/** * C program to calculate factorial of any number */ #include <stdio.h> int main() { int i, num; long long fact=1; /* Reads a number from user */ printf("Enter any number to calculate factorial: "); scanf("%d", &num); /* Runs a loop from 1 to n */ for(i=1; i<=num; i++) { fact = fact * i; } printf("Factorial of %d = %lld", num, fact); return 0; }

**Note:**We know that factorial can grow very rapidly therefore, I have used long long data type for storing factorial. Also if your compiler doesn't supports long long data type, then in case you can use long data type which is guaranteed to run on all platforms. Also if you are using long data type then you also need to replace the format specifier from %lld to %ld.

**Note:**Advance your skills by learning this program using recursive approach - C program to find factorial using recursion.

Output

Enter any number to calculate factorial: 5

Factorial of 5 = 120

Factorial of 5 = 120

Happy coding ;)

### You may also like

- Loop programming exercises index.
- C program to find all factors of a number.
- C program to find all Prime factors of a number.
- C program to find sum of first and last digit of a number.
- C program to find reverse of a number.
- C program to check Palindrome numbers.
- C program to find HCF(GCD) of two numbers.
- C program to find LCM of two numbers.
- C program to check Prime number.
- C program to print Fibonacci series up to n terms.
- C program to print Pascal triangle up to n rows.

< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).