CS 1073 Introductory Programming
for Scientific Applications
Practice with Loops: Answers


Overview: This page gives answers to the Practice with Loops questions. The additions and changes needed will be given in red. In each case you should actually try out your solution in NetBeans before looking at my answer.


First Starting Point: Loop to print numbers 1 to 20: Here are two equivalent versions of a loop to print the numbers from 1 to 20 inclusive on a line with spaces between them:

int i = 1;
while (i <= 20) {
   System.out.print(i + " ");
   i++;
}
for (int i = 1; i <= 20; i++) {
   System.out.print(i + " ");
}
Common output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Practice Exercises:

  1. Change the loops so that they print only the even numbers between 1 and 20, that is, 2 4 6 8 10 12 14 16 18 20.

    int i = 2;
    while (i <= 20) {
       System.out.print(i + " ");
       i = i + 2;
    }
    
    for (int i = 2; i <= 20; i= i + 2) {
       System.out.print(i + " ");
    }
    
    Common output: 2 4 6 8 10 12 14 16 18 20

    int i = 1;
    while (i <= 20) {
       if (i%2 == 0)
          System.out.print(i + " ");
       i++;
    }
    
    for (int i = 1; i <= 20; i++) {
       if (i%2 == 0)
          System.out.print(i + " ");
    }
    
    Common output: 2 4 6 8 10 12 14 16 18 20

  2. Change the loops so that they print those numbers divisible by 3, that is, 3 6 9 12 15 18.

    int i = 3;
    while (i <= 18) {
       System.out.print(i + " ");
       i = i + 3;
    }
    
    for (int i = 3; i <= 18; i = i + 3) {
       System.out.print(i + " ");
    }
    
    Common output: 3 6 9 12 15 18

    int i = 1;
    while (i <= 20) {
       if (i%3 == 0)
          System.out.print(i + " ");
       i++;
    }
    
    for (int i = 1; i <= 20; i++) {
       if (i%3 == 0)
          System.out.print(i + " ");
    }
    
    Common output: 3 6 9 12 15 18

  3. Change the loops so that they print the square of each odd number, that is, 1 9 25 49 81 121 169 225 289 361.

    int i = 1;
    while (i <= 19) {
       System.out.print(i*i + " ");
       i = i + 2;
    }
    
    for (int i = 1; i <= 19; i = i + 2) {
       System.out.print(i*i + " ");
    }
    
    Common output: 1 9 25 49 81 121 169 225 289 361

    int i = 1;
    while (i <= 20) {
       if (i%2 == 1)
          System.out.print(i*i + " ");
       i++;
    }
    
    for (int i = 1; i <= 20; i++) {
       if (i%2 == 1)
          System.out.print(i*i + " ");
    }
    
    Common output: 1 9 25 49 81 121 169 225 289 361

  4. Change the loops so that they print all 20 numbers with plus signs between them and a blank on either side, but with no plus sign at the end, that is, 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20.

    System.out.print("1");
    int i = 2;
    while (i <= 20) {
       System.out.print(" + " + i);
       i++;
    }
    
    System.out.print("1");
    for (int i = 2; i <= 20; i++) {
       System.out.print(" + " + i);
    }
    
    Common output: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20

    int i = 1;
    while (i <= 20) {
       System.out.print(i);
       if (i < 20) System.out.print(" + ");
       i++;
    }
    
    for (int i = 1; i <= 20; i++) {
       System.out.print(i);
       if (i < 20) System.out.print(" + ");
    }
    
    Common output: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20


Second Starting Point: Loop to add numbers 1 to 20: Here we are adding the numbers from 1 to 20, using a running sum in the variable sum:

int sum = 0;
int i = 1;
while (i <= 20) {
   sum = sum + i;
   i++;
}
System.out.println(sum);
int sum = 0;
for(int i = 1; i <= 20; i++) {
   sum = sum + i;
}
System.out.println(sum);
Common output: 210

Practice Exercises:

  1. Change the loops so that they add up the even numbers between 1 and 20, that is, 2 4 6 8 10 12 14 16 18 20 to get an answer of 110.

    int sum = 0;
    int i = 2;
    while (i <= 20) {
       sum = sum + i;
       i = i + 2;
    }
    System.out.println(sum);
    
    int sum = 0;
    for(int i = 2; i <= 20; i = i + 2) {
       sum = sum + i;
    }
    System.out.println(sum);
    
    Common output: 210

    int sum = 0;
    int i = 1;
    while (i <= 20) {
       if (i%2 == 0)
          sum = sum + i;
       i++;
    }
    System.out.println(sum);
    
    int sum = 0;
    for(int i = 1; i <= 20; i++) {
       if (i%2 == 0)
          sum = sum + i;
    }
    System.out.println(sum);
    
    Common output: 210

  2. Change the loops so that they go through the odd integers (from 1 to 19), keeping track of the sum of these integers at each stage, and printing the sum at each stage, so the output should be: 1 4 9 16 25 36 49 64 81 100.

    int sum = 0;
    int i = 1;
    while (i <= 19) {
       sum = sum + i;
       System.out.print(sum + " ");
       i = i + 2;
    }
    
    int sum = 0;
    for(int i = 1; i <= 19; i = i + 2) {
       sum = sum + i;
       System.out.print(sum + " ");
    }
    
    Common output: 1 4 9 16 25 36 49 64 81 100

    int sum = 0;
    int i = 1;
    while (i <= 20) {
       if (i%2 == 1) {
          sum = sum + i;
          System.out.print(sum + " ");
       }
       i++;
    }
    
    int sum = 0;
    for(int i = 1; i <= 20; i++) {
       if (i%2 == 1) {
          sum = sum + i;
          System.out.print(sum + " ");
       }
    }
    
    Common output: 1 4 9 16 25 36 49 64 81 100


Third Starting Point: Loop to add up 1 + 1/2 + 1/4 + 1/8 + 1/16 + ... + 1/1024: Here we are using an index sum from 0 to 10, adding 1/(2^i) at each stage. We are still using the variable sum for a running sum but now sum needs to be a double. We also need a new variable term that gives the term added in at each stage.

double sum = 0;
int i = 0;
double term = 1.0;
while (i <= 10) {
   sum = sum + term;
   // calculate term for next loop
   term = 0.5*term;
   i++;
}
System.out.println(sum);
double sum = 0;
double term = 1.0;
for(int i = 0; i <= 10; i++) {
   sum = sum + term;
   // calculate term for next loop
   term = 0.5*term;
}
System.out.println(sum);
Common output: 1.9990234375 (very close to 2)

Practice Exercises:

  1. Change the loops so that they add up a series that starts with 1.0/3.0, and each term is 1.0/3.0 times the previous term. (The answer should be a little less that one-half. The series looks like 1/3 + 1/9 + 1/27 + 1/81 + ... + 1/59049

    double sum = 0;
    int i = 1;
    double term = 1.0/3.0;
    while (i <= 10) {
       sum = sum + term;
       // calculate term for next loop
       term = (1.0/3.0)*term;
       i++;
    }
    System.out.println(sum);
    
    double sum = 0;
    double term = 1.0/3.0;
    for(int i = 1; i <= 10; i++) {
       sum = sum + term;
       // calculate term for next loop
       term = (1.0/3.0)*term;
    }
    System.out.println(sum);
    
    Common output: 0.49999153245609573

  2. Change the loops so that they add up a series that has each term equal to 1/i3: 1/13 + 1/23 + 1/33 + 1/43 + 1/53 + 1/63 + 1/73 + 1/83 + 1/93 + 1/103 = 1 + 1/8 + 1/27 + 1/64 + 1/125 + 1/216 + 1/343 + 1/512 + 1/729 + 1/1000.

    double sum = 0;
    int i = 1;
    double term;
    while (i <= 10) {
       term = 1.0/(i*i*i);
       sum = sum + term;
       i++;
    }
    System.out.println(sum);
    
    double sum = 0;
    double term;
    for(int i = 1; i <= 10; i++) {
       term = 1.0/(i*i*i);
       sum = sum + term;
    }
    System.out.println(sum);
    
    Common output: 1.197531985674193


Fourth Starting Point: Loop to form the alternating sum 1 - 1/2 + 1/4 - 1/8 + 1/16 - ... + 1/1024: Here we are adding ((-1)^i)/(2^i) at each stage. Here we add yet another variable sign that flips back and forth from 1 to -1 and back each time the loop is executed.

double sum = 0;
int i = 0;
double term = 1.0;
double sign = 1.0;
while (i <= 10) {
   sum = sum + sign*term;
   // calculate term and sign for next loop
   term = 0.5*term;
   sign = -sign;
   i++;
}
System.out.println(sum);
double sum = 0;
double term = 1.0;
double sign = 1.0;
for(int i = 0; i <= 10; i++) {
   sum = sum + sign*term;
   // calculate term and sign for next loop
   term = 0.5*term;
   sign = -sign;
}
System.out.println(sum);
Common output: 0.6669921875 (very close to 2/3)

Practice Exercises:

  1. Change the loops so that they add up a series that starts with 1.0/3.0, and each term is 1.0/3.0 times the previous term, and finally, the signs alternate. (The answer should be a little less that one-quarter. The series looks like 1/3 - 1/9 + 1/27 - 1/81 + ... - 1/59049. Notice that this series doesn't have a term for i == 0.)

    double sum = 0;
    int i = 1;
    double term = 1.0/3.0;
    double sign = 1.0;
    while (i <= 10) {
       sum = sum + sign*term;
       // calculate term and sign for next loop
       term = (1.0/3.0)*term;
       sign = -sign;
       i++;
    }
    System.out.println(sum);
    
    double sum = 0;
    double term = 1.0/3.0;
    double sign = 1.0;
    for(int i = 1; i <= 10; i++) {
       sum = sum + sign*term;
       // calculate term and sign for next loop
       term = (1.0/3.0)*term;
       sign = -sign;
    }
    System.out.println(sum);
    
    Common output: 0.24999576622804787

  2. Change the loops so that they add up a series that has each term equal to 1/i3, and the signs alternate: 1/13 - 1/23 + 1/33 - 1/43 + 1/53 - 1/63 + 1/73 - 1/83 + 1/93 - 1/103 = 1 - 1/8 + 1/27 - 1/64 + 1/125 - 1/216 + 1/343 - 1/512 + 1/729 - 1/1000. Notice that this series doesn't have a term for i == 0.)

    double sum = 0;
    int i = 1;
    double term; // can do without this variable
    double sign = 1.0;
    while (i <= 10) {
       term = 1.0/(i*i*i);
       sum = sum + sign*term;
       // calculate sign for next loop
       sign = -sign;
       i++;
    }
    System.out.println(sum);
    
    double sum = 0;
    double term; // can do without this variable
    double sign = 1.0;
    for(int i = 1; i <= 10; i++) {
       term = 1.0/(i*i*i);
       sum = sum + sign*term;
       // calculate sign for next loop
       sign = -sign;
    }
    System.out.println(sum);
    
    Common output: 0.9011164764149339