+ 2
My code works, but only for certain test cases
I wrote this code on HackerRank and two of the six test cases were wrong. I can't spot my mistake, but I suppose it's in the factorial calculation part. I'm already trying to find a solution but I appreciate any help. Thanks in advance. Edit: now only one test case is wrong. https://code.sololearn.com/c7VarWZ5NKzs
2 Respuestas
+ 4
You are multiplying number by zero every time since nFactorial and kFactorial is 0. Try initiating nFactorial and kFactorial as 1.
+ 3
Sanjyot21 made a correct point,
In addition you don't need to use "while" just use "if".
You can use this method "function":
public static int factorial(int x)
{
int res = 1;
for (int i=2; i<=x; i++) res *= i;
return res;
}