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

Appearance settings

Latest commit

 

History

History
History
47 lines (33 loc) · 1.22 KB

File metadata and controls

47 lines (33 loc) · 1.22 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// A single example of recursion.
class Factorial { // The factorial of five (5 x 4 x 3 x 2 x 1) is 120. Four factorial (4 x 3 x 2 x 1) is 24.
// This is a recursive function.
int factR(int n){
int result;
if(n == 1) return 1;
// Execute the recursive call to factR().
result = factR(n-1) * n; // The factorial of a number n is n*n-1 *n-2*... *1.
return result;
}
// This is an iterative equivalent.
int factI(int n) {
int t, result;
result = 1;
for(t = 1 ; t <= n ; t++) result *= t ;
return result;
}
}
public class Recursion {
public static void main(String[] args) {
// TODO Auto-generated method stub
Factorial factorial = new Factorial();
System.out.println("Factorials using recursive method.");
System.out.println("Factorial of 3 is " + factorial.factR(3));
System.out.println("Factorial of 4 is " + factorial.factR(4));
System.out.println("Factorial of 5 is " + factorial.factR(5));
System.out.println();
System.out.println("Factorials using iterative method.");
System.out.println("Factorial of 3 is " + factorial.factI(3));
System.out.println("Factorial of 4 is " + factorial.factI(4));
System.out.println("Factorial of 5 is " + factorial.factI(5));
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.