TailChain_Recursion2
  • System.out.println("factorial: "+ recursion(y,12));
    run recursion for 12 times with an integer input.
  • x = n1 + recursion(n1,n2-1) ;
  • Anticipated results:
    12 = tail processed 6 * 2 = 12
    18 = tail processed 6 * 3 = 18
    24 = tail processed 6 * 4 = 24
    30 = tail processed 6 * 5 = 30
    36 = tail processed 6 * 6 = 36
    42 = tail processed 6 * 7 = 42
    48 = tail processed 6 * 8 = 48
    54 = tail processed 6 * 9 = 54
    60 = tail processed 6 * 10 = 60
    66 = tail processed 6 * 11 = 66
    72 = tail processed 6 * 12 = 72
Code:

package javatemplate1;
// JavaFx tutorials:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class JavaTemplate1 {

static int x= 1;
public static void main(String[] args) throws IOException {
// TODO code application logic here

System.out.println("main block executing ");
List li1 = new LinkedList();
li1.add(1);li1.add(2);li1.add(3);li1.add(4);li1.add(5);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Type number :");
String str = "";
str = br.readLine();
int y = Integer.parseInt(str);

System.out.println("factorial: "+ recursion(y,12));
} // end main

private static int recursion ( int n1, int n2 )
{

if(n2==1) { return n1; }
else {
x = n1 + recursion(n1,n2-1) ;
print(n1, n2);
return x;}
}

//
public static void print(int n1 , int n2)
{
System.out.println(x + " = tail processed "+n1 + " * " + n2 +" = " + (n1* n2)); }

}
 

Displays: