Skip to content

Commit 900df06

Browse files
Merge pull request #51 from IamBisrutPyne/IamBisrutPyne-factorial-kotlin
Update factorial.kt
2 parents 6e79a05 + 7d41418 commit 900df06

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed
Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,36 @@
1-
# Title: Iterative factorial
2-
# Topic: Basics
3-
# Language: kotlin
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: Kotlin
10+
//
11+
// Time Complexity: O(n)
12+
// Space Complexity: O(1)
13+
// -----------------------------
514

6-
// Iterative factorial - placeholder in kotlin
15+
/**
16+
* Calculates the factorial of a non-negative integer.
17+
* @param n The number to calculate the factorial of.
18+
* @return The factorial result.
19+
*/
20+
fun factorial(n: Int): Long {
21+
// Use Long to handle larger results safely
22+
if (n < 0) {
23+
return 0
24+
}
25+
var result: Long = 1
26+
for (i in 2..n) {
27+
result *= i
28+
}
29+
return result
30+
}
31+
32+
fun main() {
33+
val num = 10
34+
println("Factorial of $num is: ${factorial(num)}")
35+
// Output: Factorial of 10 is: 3628800
36+
}

0 commit comments

Comments
 (0)