Showing posts with label passing objects. Show all posts
Showing posts with label passing objects. Show all posts

Wednesday, February 24, 2016

Java Program to calculate factorial by passing object




public class FindFactorial {
int a=0;
  FindFactorial(int i){
    a=i;
  }
  int displayFact(FindFactorial f){
    if(f.a<1)
      return 1;
    else{
         f.a--;
         return (f.a+1)*displayFact(f);
     }
  }

}

 

public class Main {
public static void main(String[] args){
      FindFactorial f1 = new FindFactorial(3);
      System.out.println("factorial ="+ f1.displayFact(f1));
}
}


Output: