File tree Expand file tree Collapse file tree 1 file changed +35
-5
lines changed
Expand file tree Collapse file tree 1 file changed +35
-5
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments