Friday 4 April 2014

How to calculate factorial of a number using recursive methods in Java


import java.io.*;
class factorial
{
public static long getfactorial(int n)
{
if(n<=0)
return 1;
return n * getfactorial(n-1);
}
public static void main(String[] args) throws IOException
{
BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
System.out.print("\nEnter number : ");
int n=Integer.parseInt(rd.readLine());
System.out.print("\nFactorial of " + n + " = " + getfactorial(n));
}
}

No comments: