CHAPTER 7 PROGRESS CHECKS

1. Identify all the errors in the following repetition statements. Some errors are syntactical while others are logical (e.g., infinite loops).

a. for (int i = 10; i > 0; i++) {

x = y;

a = b;

   }

 

b.     sum = 0;

do {

num = inputBox.getInteger();

sum += num;

} until (sum > 10000);

 

c.     while (x < 1 && x > 10) {

a = b;

}

 

d.     while (a == b) ;

{

a = b;

x = y;

}

 

e.     for (int i = 1.0; i <= 2.0; i += 0.1) {

x = y;

a = b;

}

 

2. Write for, do–while, and while statements to compute the following sums and products.

  1. 1 +2 +3 +···+100
  2. 5 +10 +15 +···+50
  3. 1 +3 +7 +15 +31 +···+(2 20 -1)

d. 1 + ½ + 1/3 + ¼ + ···+ 1/15

e. 1 × 2 × 3 × ···× 20

f. 1 × 2 × 4 × 8 × ···× 2^ 20

7.3. What will be the value of sum after each of the following nested loops is executed?

a.     sum = 0;

for (int i = 0; i <= 10; i++)

for (int j = 0; j <= 10; j++)

sum += i ;

b.     sum = 0;

j = 0;

do {

j++;

for (int i = 5; i > j; i--)

sum = sum + (i+j);

} while (j < 11);

c.     sum = 0;

i = 0;

while (i < 5) {

j = 5;

while (i != j) {

sum += j;      

j--;

}             

i++;

}

d.     sum = 0;

for (int i = 0; i <= 10; i++)

for (int j = 10; j > 2*i; j--)

sum = sum + (j - i);

 

4. Determine the output from the following code.

num = 123;

for (int i = 0; i <= 5; i++) {

display.printLine( Format.centerAlign(5 + 2*i, num) );

display.skipLine( (int) Math.ceil(i/2) );

}

for (int j = 0; j <= 6; j += 2) {

display.printLine( Format.rightAlign(15-j,num) );

display.skipLine( (int) Math.ceil(j % 2) );

}

 

5. Rewrite the following nested-for statements using nested do–while and while statements.

sum = 0;

number = 0;

for (int i = 0; i <= 10; i++)

for (int j = 10; j >= i; j--) {

number++;

sum = sum + (j - i);

}     

b. product= 1;

number =0;

for (int i =1; i <5; i++)

            for (int j=1; j<5; j++){

                        number ++;

                        product+= number;

}

 

6. Write an application to print out the numbers 10 through 49 in the following manner:

 

10 11 12 13 14 15 16 17 18 19

20 21 22 23 24 25 26 27 28 29

30 31 32 33 34 35 36 37 38 39

40 41 42 43 44 45 46 47 48 49

 

How would you do it with a switch, a nested for loop and using modulo arithmetic?

 

7. A prime number is an integer greater than one and divisible only by itself and one. The first seven prime numbers are 2, 3, 5, 7, 11, 13, and 17. Write a method that returns true if its argument is a prime number. Write another method that prints out all factors of the argument.

 

8. A perfect number is a positive integer that is equal to the sum of its proper divisors. A proper divisor is a positive integer other than the number itself that divides the number evenly (i.e., no remainder). For example, six is a perfect number because the sum of its proper divisors 1, 2, and 3 is equal to 6. Eight is not a perfect number because 1 +2 +4 !=8. Write an application that accepts a positive integer and determines whether the number is perfect. Also, display all proper divisors of the number. Try a number between 20 and 30 and another number between 490 and 500.

 

9. Write a method that returns the number of digits in an integer argument; for example, 23,498 has five digits.

 

10. Write a recursive method to compute the sum of the first N positive integers. Note: This is strictly for exercise. You should not write the real method recursively.

 

11. (Optional) Write a recursive method to compute the sum of the first N positive odd integers. Note: This is strictly for exercise. You should not write the real method recursively.