A method in java that calls itself is called recursive method.
–>Syntax:-
returntype methodname()
{
//code to be executed
methodname(); //calling same method
}
Example:-
Public Class MyClass
{
// N!=N*(N-1)*(N-2)*………*1.
// 5=5*4*3*2*1
public static int factorial (int N)
{
if(N<=1)
return 1;
else (N*factorial(N-1)); //Call itself method
}
public static void main(string[] args)
System.out println(factorial(5));
}
}
0 Comments:
Post a Comment