// Recursive approach
public int getFactorialRecursive(int n) {
if(n==0) return 1;
else
return n * getFactorial(n-1);
}
// Non Recursive approach
public int getFactorial(int n) {
int factorial=1;
if(n==0) return factorial;
while(n!=0) {
factorial=factorial*n;
n--;
}
return factorial;
}
No comments:
Post a Comment