Monday, October 19, 2009

A program find the factorial of a number using recursive functions (Java)

class Factorial 
{

 public static int factorialRec(int no)
 {
  int ans;
  if(no==1)
   return 1;
  ans=no*(factorialRec(no-1));
  return ans;
 }

 public static void main(String[] args) 
 {
  int fact,num;
  String number=javax.swing.JOptionPane.showInputDialog("Enter  Number");
  num=Integer.parseInt(number);
  fact=factorialRec(num);
  System.out.println("Factorial of number::"+fact);
 }
}

No comments:

Post a Comment