ub Logo

Let's get started with a program that uses a while loop!

  1. Open Eclipse
  2. Right-Click on your buffalo.edu.fa23..your-username
  3. Choose: File -> New -> class
  4. In the Name: textbox type: Thirteens
  5. Click: Finish
  6. Copy the Thirteens.java Template (Java) code (below)
  7. Paste that code into your new Thirteens class in the editor
  8. Change both occurrences of put-your-username-here
  9. Replace blank on these lines:
    int[] thirteens = new int[__];
    while (numFound < __) {
      System.out.println("First __ multiples of 13:");
      for (int i = 0; i < __; i++) {

    line with an integer between 5 and 20
  10. What would be a better way to declare the array size?
  11. Click the Run icon

Thirteens.java Template (Java)

package buffalo.edu.fa23..put-your-username-here;

/**
 *
 * File: Thirteens.java
 *
 * Create a Java class named Thirteens that prints out a 13s multiplication table.
 * 
 * 1) This class contains a main( ) method which creates an 
 *    array to hold the multiples of 13 then walks through
 *    that array and prints the results
 *
/**
 *
 * @author put-your-name-here
 *
 */

public class Thirteens {

  public static void main( String args[] ) {
    int[] thirteens = new int[__]; // declare an array to hold multiples of 13
    int numFound = 0;
    // candidate: the number that might be a multiple of 13
    int candidate = 1;
    // build array that holds multiples of 13
    while (numFound < __) {
      if (candidate % 13 == 0) {
        thirteens[numFound] = candidate;
        numFound++;
      }
      candidate++;
    }
    System.out.println("First __ multiples of 13:");
    // walk through array that holds multiples of 13
    for (int i = 0; i < __; i++) {
      System.out.println("13 * " + (i + 1) + " = " + thirteens[i]);
    }
  }
}

Triangle Class Output

First 5 multiples of 13:
13 * 1 = 13
13 * 2 = 26
13 * 3 = 39
13 * 4 = 52
13 * 5 = 65