a program in c with detail explanation to calculate the factorial of a given number using a for loop.

Mathematical Notation
In mathematical notation, the factorial is marked with an exclamation mark "!" .
The formula for the factorial calculation n is expressed as follows:
n! = n x (n-1) x (n-2) x (n-3) x ... x 1

Factorial Program in C using For Loop
This method shows how to calculate the factorial of a number using a for loop in C.
Factorials are important mathematical operations, and this method provides a practical way to calculate it.
Program/Source Code
Here is the source code of a C program to print the factorial of a given number using a loop (iterative solver). The C program has been compiled and executed successfully. The output of the program is shown below.


C program to print the factorial of a given number using for loop

#include <stdio.h> int main() { // variable fact to store factorial of number N int fact = 1, n; // Accept input printf("Enter the number: \n"); scanf("%d", &n); // validating number N if (n <= 0) printf(“\n Please Enter positive Number”); // loop or iterate N times and multiply all positive numbers else { for (int i=1;i<=n;i++) { fact=fact*i; } } // Print the factorial fact. printf("\nFactorial of %d = %5d\n", n, fact); return(0); }

Output
Please Enter positive Number
5
Factorial of 5 =120
Program Explanation
1. input any number and store it in "n".
2. Check if the number is valid or not.
3. If the number is invalid, print a message.
4. Otherwise execute a for loop from 1 to N.
5. Take the variable "fact" and store the total product of N numbers in it.
6. Display the factorial value.
Example:
Let's look at an example where "n" is 7.
First we check if "n" is less than or equal to 0, but it is not. We then loop from 1 to 7, multiplying the numbers along the way and storing the result in variable "fact".
The final result is a factorial of 7 equal to 5040.


Previous :-->> 19.Write a program in c language to print the pattern of a right angled triangle using a for loop.
 -->> NEXT: Arrays assignments in c language
-->>ALL Loops Assignments in c