Exercises (Sections C.14–C.20)

C.22 Describe the four basic elements of counter-controlled repetition.

C.23 (Find the Smallest Value) Write an application that finds the smallest of several integers. Assume that the first value read specifies the number of values to input from the user.

C.24 Assume that i = 1, j = 2, k = 3 and m = 2. What does each of the following statements print?

a) System.out.println( i == 1 );

b) System.out.println( j == 3 );

c) System.out.println( ( i >= 1 ) && ( j < 4 ) );

d) System.out.println( ( m <= 99 ) & ( k < m ) );

e) System.out.println( ( j >= i ) || ( k == m ) );

f) System.out.println( ( k + m < j ) | ( 3 - j >= k ) );

g) System.out.println( !( k > m ) );

C.25 (Calculating the Value of π) Calculate the value of π from the infinite series

Image

Print a table that shows the value of π approximated by computing the first 200,000 terms of this series. How many terms do you have to use before you first get a value that begins with 3.14159?

C.26 What does the following program segment do?

for ( i = 1; i <= 5; ++i )
{
   for ( j = 1; j <= 3; ++j )
   {
      for ( k = 1; k <= 4; ++k )

         System.out.print( '*' );
      System.out.println();
   } // end inner for

   System.out.println();
} // end outer for

C.27 (“The Twelve Days of Christmas” Song) Write (as concisely as possible) an application that uses repetition and one or more switch statements to print the song “The Twelve Days of Christmas.”

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset