You are on page 1of 2

Dwi Putri Wahyuningsih

2019390004

Assignment - 2: Type of Recursion

Write all type of recursive methods (about 20 methods), describe each of them using your own
word, and give a brief code example.

1. Linear Recursive
A linear recursive function is one that only makes one call to itself each time the function
is run (as opposed to a function that calls itself multiple times during its execution).
Factorial functions are good examples of linear recursion.
Code:
import java.util.Scanner;

public class Factorial {

public static void main(String[] args)


{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number whose factorial
is to be found: ");
int n = scanner.nextInt();
int result = factorial(n);
System.out.println("The factorial of " + n + " is
" + result);
}

public static int factorial(int n)


{
int result = 1;
for (int i = 1; i <= n; i++)
{
result = result * i;
}

return result;
}
}
Result

2.

You might also like