Skip to content
Advertisement

Why is this program giving “division by zero” error?

Give one point to A[i] if either A[i]%A[j] ==0 or A[j]%A[i] == 0 Calculating the total points received by every element. Input: A={2,3,4,5,6} Output: 2,1,1,0,2 I am getting this error message: Exception in thread “main” java.lang.ArithmeticException: / by zero

JavaScript
JavaScript

Advertisement

Answer

When you are running the code, this line:

JavaScript

Can set count = 0. So when the value at slot 1 will be used, it will throw the error.

For “where is the error”, java has pretty error informations, and it will return something like:

JavaScript

And you will see that the line 61 is:

JavaScript

I suggest you to check when it’s 0, or to don’t set 0 count like check if it’s 0, and set 1, for example:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement