INTRODUCTION TO COMPUTER SCIENCE
Robert Sedgewick and Kevin Wayne


This is the syntax highlighted version of Divisors.java.

/*************************************************************************
 *  Compilation:  javac Divisors.java
 *  Execution:    java Divisors 40
 *  
 *  Prints a table where entry (i, j) is a '*' if i divides j,
 *  and '.' otherwise.
 *
 *
 *  % java Divisors 10 
 *  * . . . . . . . . . 
 *  * * . . . . . . . . 
 *  * . * . . . . . . . 
 *  * * . * . . . . . . 
 *  * . . . * . . . . . 
 *  * * * . . * . . . . 
 *  * . . . . . * . . . 
 *  * * . * . . . * . .  
 *  * . * . . . . . * . 
 *  * * . . * . . . . * 
 *
 *************************************************************************/

public class Divisors {

   public static void main(String[] args) { 
      int N = Integer.parseInt(args[0]);

      for (int i = 1; i <= N; i++) {
         for (int j = 1; j <= N; j++) {
            if (i % j == 0) System.out.print("* "); 
            else            System.out.print(". "); 
         }
         System.out.println("");
      }
   }
}


Last updated: Wed Feb 11 18:07:23 EST 2004 .
Copyright © 2004, Robert Sedgewick and Kevin Wayne.