Monday, June 24, 2013

For loop with 5 columns

package com.spconger.Quadratic;

import acm.program.*;

public class Primes extends ConsoleProgram {

 private final int MAXVALUES=41;
 private final int NUMBEROFCOLUMNS=5;
 
 public void run() {
  setSize(500,300);
  for (int i = 1; i <= MAXVALUES; i++) {
   int prime = i * i - i + MAXVALUES;
   if (i % NUMBEROFCOLUMNS != 0) {
    print(prime + "\t");
   } else {
    print(prime + "\n");
   }
  }
 }
}

No comments:

Post a Comment