Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7326eee

Browse files
factorialIterative
1 parent f6c2214 commit 7326eee

File tree

1 file changed

+16
-0
lines changed
  • algorithms/src/main/java/ivanmarkovic/algorithms/recursion

1 file changed

+16
-0
lines changed

‎algorithms/src/main/java/ivanmarkovic/algorithms/recursion/Factorial.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ public static void main(String args[]) {
66

77
for(int i = 0; i <= 6; i++)
88
System.out.println("Factorial of " + i + " is " + factorialRecursive(i));
9+
10+
System.out.println("-------------------------------------------------------");
11+
12+
for(int i = 0; i <= 6; i++)
13+
System.out.println("Factorial of " + i + " is " + factorialIterative(i));
914
}
1015

1116
public static int factorialRecursive(int n) {
@@ -15,5 +20,16 @@ public static int factorialRecursive(int n) {
1520
return n * factorialRecursive(n - 1);
1621

1722
}
23+
24+
public static int factorialIterative(int n) {
25+
if(n <= 1)
26+
return 1;
27+
int f = 1;
28+
while(n > 1) {
29+
f *= n;
30+
n--;
31+
}
32+
return f;
33+
}
1834

1935
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /