Method that invokes themselves (recursion)

class testfactorial
{
public static void main (String[] args)
{
for ( int i =0;i<9; i++)
System.out.println(“f(“+i+”) = “+f(i));
try { System.in.read();}
catch (Exception e) {}
}
static long f(int n)
{
if (n<2) return 1;
return n*f(n-1);
}
}

 

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.