RECURSION

 
Recursion is the process of defining something in terms of itself. As it relates to java programming, recursion is the attribute that allows a method to call itself. A method that calls itself is said to be recursive.
Every recursion should have the following characteristics
A simple base case which we have a solution for and a return value.
A way of getting our problem closer to the base case. I.e. a way to chop out part of the problem to get a somewhat simpler problem.
A recursive call which passes the simpler problem back into the method.
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
class Factorial
{
long fact(long a)
{
if(a <= 1)
return 1;
else
{
a= a*fact(a-1);
return a;
}
}
public static void main (String arr[]) throws IOException
{
System.out.println("Enter a number to find factorial");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int num = Integer.parseInt( br.readLine());
Factorial f = new Factorial();
System.out.println(f.fact(num));
}
}

O/P