Skip to content

Commit 6568811

Browse files
Update factorial.c
Added the Factorial program in C.
1 parent 93caf5c commit 6568811

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

snippets/Basics/c/factorial.c

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,34 @@
1-
# Title: Iterative factorial
2-
# Topic: Basics
3-
# Language: c
4-
# Example: see bottom
1+
// Metadata Header (MANDATORY)
2+
// -----------------------------
3+
// Program Title: Factorial Calculation (Iterative)
4+
// Author: IamBisrutPyne
5+
// Date: 2025-10-10
6+
//
7+
// Description: Computes the factorial of a non-negative integer using an iterative approach.
8+
//
9+
// Language: C
10+
//
11+
// Time Complexity: O(n)
12+
// Space Complexity: O(1)
13+
// -----------------------------
514

6-
// Iterative factorial - placeholder in c
15+
#include <stdio.h>
16+
17+
// Calculates n! iteratively. Using long long for potentially larger results.
18+
long long factorial(int n) {
19+
if (n < 0) {
20+
return 0;
21+
}
22+
long long result = 1;
23+
for (int i = 2; i <= n; i++) {
24+
result *= i;
25+
}
26+
return result;
27+
}
28+
29+
int main() {
30+
int num = 10;
31+
printf("Factorial of %d is: %lld\n", num, factorial(num));
32+
// Output: Factorial of 10 is: 3628800
33+
return 0;
34+
}

0 commit comments

Comments
 (0)