public class Factorial {
//this is recursive function
int fact(int n){
int result;
if(n==1)return 1;
result=fact(n-1)*n;
return result;
}
}
public class RecursionDemo {
public static void main(String[] args) {
Factorial f =new Factorial();
System.out.println("Factorial of 3 is"+f.fact(3));
System.out.println("Factorial of 4 is"+f.fact(4));
System.out.println("Factorial of 5 is"+f.fact(5));
}
}
Output:
Factorial of 3 is6
Factorial of 4 is24
Factorial of 5 is120
Factorial of 3 is6
Factorial of 4 is24
Factorial of 5 is120
0 comments:
Post a Comment