so my assignment is to create a java program that prints all numbers from 2-500 inclusively, with 10 numbers in each line. I've learned the basics of loops and a brief intro to methods. I've looked at other questions/answers, but, I can't seem to combine all the info into workable code.
int n;
for (int count = 0; count < 10; count++) {
for (n=2; n <= 500; n++){
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i != 0)
System.out.print(i + " ");
}
}
System.out.println();
}
This is all I have and I'm printing out a really weird set of numbers. I know there's something wrong with my if
statement and I know I haven't set it up to properly print out the right values, I'm just at a complete loss.
Honestly, I'm still having trouble figuring out why I only need to check i
up to the sqrt of n
.
Basically I want the line of code to somehow print out
2 3 5 7 11 13 17 19 23 29
and then jump to the next line where it will continue
etc etc etc
Is using a method the most efficient way? I was trying to avoid it because methods was only recently introduced.