Sunday, May 1, 2011

Factorial in java

Factorial of number using iteration

public int factorial(int var) {
if ((var) > 1) {
fact = var * factorial(var - 1);
return fact;
}

else {
return 1;
}
}


 

Factorial of number using recursion


public int factorial(int var) {
if ((var) > 1) {
fact = var * factorial(var - 1);
return fact;
}

else {
return 1;
}

No comments:

Post a Comment

Chitika