[Solved] Java I cannot make new lines at the right places


As per divisibility rule, if a number is a multiple of 8, it is also a multiple of 4. Hence, you need not check for modulus. Also since you know you want to increment by 8 only, you can automatically do that without incrementing one by one..

Ideally You have to do:

      int num = 24, count=0;  // Initializing the first number to be 24, since it is divisible by both 8 and 4.
      while (num <=500)
      {
          System.out.print(num +","); 
          count++; // Incrementing the record count
          num +=8; // Adding 8 to the existing number
          if (count == 10)
          {
              System.out.println(""); // A new line after every tenth record
              count=1; // Count is reset here
          }
      }

Output:

24,32,40,48,56,64,72,80,88,96,
104,112,120,128,136,144,152,160,168,
176,184,192,200,208,216,224,232,240,
248,256,264,272,280,288,296,304,312,
320,328,336,344,352,360,368,376,384,
392,400,408,416,424,432,440,448,456,
464,472,480,488,496,

3

solved Java I cannot make new lines at the right places