+ 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

5th Dec 2021, 9:28 AM
Salma D. Elabsi
Salma D. Elabsi - avatar
2 Respostas
+ 4
You are multiplying number by zero every time since nFactorial and kFactorial is 0. Try initiating nFactorial and kFactorial as 1.
5th Dec 2021, 10:12 AM
Sanjyot21
Sanjyot21 - avatar
+ 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; }
5th Dec 2021, 12:04 PM
SoloProg
SoloProg - avatar