I have been trying to code to find the LCM of given Array elements. My code looks like this
public long LcmOfArray(List<int> a) { long LCM = 1; bool divisible = false; int divisor = 2, count = 0; while (count != a.Count) { for (int i = 0; i < a.Count; i++) { if (a[i] == 0) { return 0; } if (a[i] < 0) { a[i] *= -1; } if (a[i] % divisor == 0) { divisible = true; a[i] /= divisor; } if (a[i] == 1) { count++; } } if (divisible) { LCM *= divisor; } else { divisor++; } } return LCM; }
My problem is that the output console freeze as I enter the input. I have tried another method without the while loop. That is, eliminated while loop and added an if loop in the end.
if (count == a.Count) { return LCM; }
But now compiler throws an error stating that not all code path returns a value. Can somebody help me with what is wrong in my code? I am a beginner in coding. Thanks in advance!!