CS 1073 Introductory Programming
for Scientific Applications
Practice with Loops


Overview: This page gives practice with loops as preparation for Quiz 2. The practice questions will be numbered in red.

In each case you should actually try out your solution in NetBeans before looking at my answer. (You get almost no benefit from just looking at the 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.

  2. Change the loops so that they print those numbers divisible by 3, that is, 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.

  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.


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.

  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.


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

  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.


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.)

  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.)