Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Whenever I run this code:

package jmv;

public class euler3 {
    int x=0;
    public static void main(String[] args) {
        for (int x=0 ; x < 6008 ; x++){
            if(6008 % x == 0){
                System.out.println(x);
            }
        }

    }
}

I receive this error:

Exception in thread "main" java.lang.ArithmeticException: / by zero
    at euler2.euler2.main(euler2.java:7)

What does this mean and how do I fix it?

share|improve this question
4  
for (int x=1 ; x < 6008 ; x++){ –  Mirek Surma yesterday
1  
start your loop with x=1. Java doesn't allow you to divide by 0 zero because is undefined –  shazin yesterday
    
you get that error because you are dividing a number by Zero which result to undefined. Try to start your loop to int x = 1 –  Niang yesterday
    
To those of us who've been working in Java and related languages for a while that error message is completely obvious. I had not even noticed on a first reading that it used the symbol / rather than the word divide. To those down-voting, try to think back to when all this was new to you. –  djna 23 hours ago
add comment

5 Answers

The error is exactly what it states - you can't divide by zero. In your case, when you do if(6008 % x == 0) you are taking the modulus of 6008 and 0 - that's undefined. Try x=1 instead of x=0

share|improve this answer
add comment

You set

int x=0

and then compute the modulus, that is the remainder when dividing by x

6008 % x

So effectively you are dividing by zero, which is impossible. Try starting with x = 1;

share|improve this answer
add comment

Your code started x at 0. That means, your code will try to run 6008 % 0, which causes the division by zero error.

Change int x = 0 to int x = 1 to fix that.

share|improve this answer
add comment

This is due to division by zero as the modulo operator (%) is essentially a divide.

Start from x=1 instead.

  public static void main(String[] args) {
        for (int x=1 ; x < 6008 ; x++){
            if(6008 % x == 0){
                System.out.println(x);
            }
        }

    }
share|improve this answer
add comment

start x from 1 so that 6008 % 0 The first iteration of loop which is causing error can be avoided..

Note: If zero is must add/print it manually before loop starts.

public class euler3 {
     public static void main(String[] args) {
        for (int x=1 ; x < 6008 ; x++){
            if(6008 % x == 0){
                System.out.println(x);
            }
        }
     }
}
share|improve this answer
    
A tiny bit of text explaining that changing the loop so that it starts at 1 avoids division by zero would make this a better answer. –  Jason Aller 23 hours ago
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.