Reputation: 573
I am trying to make a prime number finder in Java, and I am aware that there are plenty already out there. I am getting a calculation error when I run it, and no 'quick fixes.'
Here is the error: "Exception in thread "main" java.lang.ArithmeticException: / by zero at PrimeNumber.main(PrimeNumber.java:12)"
Here is the code:
public class PrimeNumber {
public static void main(String rane[]){
int f1 = 0;
int f2 = 0;
int m = 0;
int p = 0;
loop:
for (p = f1 % f2; p > 0; f1 += 1){
if (f1 < 50){
//Do nothing
}else{
f2 += 1;
if (f2 > 50){
System.out.println("Done!");
break;
}
}
if (p == m){
f1 += 1;
f2 += 2;
m = 0;
break loop;
}
}
}
}
Please let me know if I can shorten this code or make it better. Thanks!
Upvotes: 1
Views: 100
Reputation: 941
Well the first iteration of the for loop calculates
p = f1 % f2
but f2 is 0. And for calculating the modulo a division is made. Why not start out with meaningful values?
Upvotes: 2