DEV3.2 TP'S
This commit is contained in:
27
DEV/DEV3.2/TP02/Exercise1/Recursion.java
Normal file
27
DEV/DEV3.2/TP02/Exercise1/Recursion.java
Normal file
@@ -0,0 +1,27 @@
|
||||
public class Recursion {
|
||||
|
||||
public static void main(String[] args) {
|
||||
if(args.length > 0) {
|
||||
displayFactorial(Integer.parseInt(args[0]));
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println("Usage: java Recursion <number>");
|
||||
}
|
||||
|
||||
public static void displayFactorial(int n) {
|
||||
System.out.println(n + "! = " + factorial(n, 0));
|
||||
}
|
||||
|
||||
private static int factorial(int n, int count) {
|
||||
count++;
|
||||
System.out.println("n = " + n + " | count = " + count);
|
||||
|
||||
if(n == 0) {
|
||||
//Thread.dumpStack();
|
||||
return 1;
|
||||
}
|
||||
|
||||
return n * factorial(n - 1, count);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user