gcp Logo

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

  1. Login to your Repl.It account.
  2. Click on: Languages
  3. In the Popular section choose: Java: A concurrent, class-based, statically typed object-oriented language.
  4. Click the pencil icon next to your repl name.
  5. In the Default name: input box type: Thirteens
  6. Delete all the lines of code provided as an example.
  7. Copy the Thirteens.java Template (Java) code (below)
  8. Paste that code into your new Thirteens class in the editor
  9. Change the @author put-your-username-here
  10. 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
  11. What would be a better way to declare the array size?
  12. Click the Run icon

Thirteens.java Template (Java)

/**
 *
 * 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 Main {

  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