Sunday, September 2, 2012

Finding Factorial Using Recursive function in JAVA

Simple and Easy Java program to find factorial of a number in Java, using recursive function.


public class Factorial
{
 public static int factorial(int no)
 {
  int fact;
  if(no==1)
   return 1;
  else
   fact=no*factorial(no-1);
  return fact;
 }

 public static void main(String args[])
 {
  if(args.length!=1)
  {
   System.out.println("1 argument required..");
   return;
  }
  int no=Integer.parseInt(args[0]);
  int fact;
  fact=factorial(no);
  System.out.println("Factorial is : "+fact);
 }
}


EmoticonEmoticon