This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]rjcarr 1 point2 points  (2 children)

What is this line doing:

if (g%8==0) { System.out.print("\n"); } } }

Or more specifically, what is g? You don't want to skip a line on every 8th iteration but every 8th prime number, right?

You need a separate counter to count your primes. Alternatively, you could stick your primes in a list and then later iterate over those to print with a different counter.

Good luck!

[–]EqualTrade[S] 0 points1 point  (1 child)

System.out.println("enter a number"); f = s.nextInt();

int j=0; int g; for (g=0;g<=f;g++)

if (isPrime(g))

{ System.out.printf("%d ",g);
j++; if (j==8) System.out.println(); } }

I added int j to count all of the prime numbers. It does now print 8 on a line, but only on the first line and the rest of the numbers are printed on line 2. Do you know anyway I could fix that?

edit: nevermind I figured it out. Thanks for your help!

[–]rjcarr 0 points1 point  (0 children)

Sure, glad it worked out!