advertisement

2.19 (Arithmetic, Largest Value and Smallest Value) Write a program that inputs three different integers from the keyboard, then prints the sum, the average, the product, the smallest and the largest of these numbers. Use only the single-selection form of the if statement you learned in this chapter. The screen dialogue should appear as follows: Input three different integers: 13 27 14 Sum is 54 Average is 18 Product is 4914 Smallest is 13 Largest is 27 2.20 (Diameter, Circumference and Area of a Circle) Write a program that reads in the radius of a circle and prints the circle’s diameter, circumference and area. Use the constant value 3.14159 for PI. Perform each of these calculations inside the printf statement(s) and use the conversion specifier %f. [Note: In this chapter, we have discussed only integer constants and variables. In Chapter 3 we’ll discuss floating-point numbers, i.e., values that can have decimal points. 2.24 (Odd or Even) Write a program that reads an integer and determines and prints whether it is odd or even. [Hint: Use the remainder operator. An even number is a multiple of two. Any multiple of two leaves a remainder of zero when divided by 2. 3.11 Identify and correct the errors in each of the following. [Note: There may be more than one error in each piece of code.] a) if ( age >= 65 ); printf( "Age is greater than or equal to 65\n" ); else printf( "Age is less than 65\n" ); b) int x = 1, total; while ( x <= 10 ) { total += x; ++x; } c) While ( x <= 100 ) total += x; ++x; d) while ( y > 0 ) { printf( "%d\n", y ); ++y; } 3.13 What does the following program print? #include <stdio.h> 2 3 int main( void ) 4{ 5 int x = 1, total = 0, y; 6 7 while ( x <= 10 ) { 8 y = x * x; 9 printf( "%d\n", y ); 10 total += y; 11 ++x; 12 } /* end while */ 13 14 printf("Total is %d\n", total); 15 return 0; 16 } /* end main */ 3.22 (Predecrementing vs. Postdecrementing) Write a program that demonstrates the difference between predecrementing and postdecrementing using the decrement operator --. 3.23 (Printing Numbers from a Loop) Write a program that utilizes looping to print the numbers from 1 to 10 side by side on the same line with three spaces between numbers. 4.2 State whether the following are true or false. If the answer is false, explain why. a) The default case is required in the switch selection statement. b) The break statement is required in the default case of a switch selection statement. c) The expression (x > y && a < b) is true if either x > y is true or a < b is true. d) An expression containing the || operator is true if either or both of its operands is true. 4.3 Write a statement or a set of statements to accomplish each of the following tasks: a) Sum the odd integers between 1 and 99 using a for statement. Assume the integer variables sum and count have been defined. b) Print the value 333.546372 in a field width of 15 characters with precisions of 1, 2, 3, 4 and 5. Left justify the output. What are the five values that print? c) Calculate the value of 2.5 raised to the power of 3 using the pow function. Print the result with a precision of 2 in a field width of 10 positions. What is the value that prints? d) Print the integers from 1 to 20 using a while loop and the counter variable x. Assume that the variable x has been defined, but not initialized. Print only five integers per line. [Hint: Use the calculation x % 5. When the value of this is 0, print a newline character, otherwise print a tab character.] e) Repeat Exercise 4.3 (d) using a for statement. 4.4 Find the error in each of the following code segments and explain how to correct it. a) x = 1; while ( x <= 10 ); x++; } b) for ( y = .1; y != 1.0; y += .1 ) printf( "%f\n", y ); c) switch ( n ) { case 1: printf( "The number is 1\n" ); case 2: printf( "The number is 2\n" ); break; default: printf( "The number is not 1 or 2\n" ); break; } d) The following code should print the values 1 to 10. n = 1; while ( n < 10 ) printf( "%d ", n++ ); 4.5 Find the error in each of the following. (Note: There may be more than one error.) a) For ( x = 100, x >= 1, x++ ) printf( "%d\n", x ); b) The following code should print whether a given integer is odd or even: switch ( value % 2 ) { case 0: printf( "Even integer\n" ); case 1: printf( "Odd integer\n" ); } c) The following code should input an integer and a character and print them. Assume the user types as input 100 A. scanf( "%d", &intVal ); charVal = getchar(); printf( "Integer: %d\nCharacter: %c\n", intVal, charVal ); d) for ( x = .000001; x == .0001; x += .000001 ) { printf( "%.7f\n", x ); } e) The following code should output the odd integers from 999 to 1: for ( x = 999; x >= 1; x += 2 ) { printf( "%d\n", x ); } f) The following code should output the even integers from 2 to 100: counter = 2; Do { if ( counter % 2 == 0 ) { printf( "%d\n", counter ); } counter += 2; } While ( counter < 100 ); g) The following code should sum the integers from 100 to 150 (assume to 0): total is initialized for ( x = 100; x <= 150; x++ ); { total += x; } 4.7 Write for statements that print the following sequences of values: a) 1, 2, 3, 4, 5, 6, 7 b) 3, 8, 13, 18, 23 c) 20, 14, 8, 2, –4, –10 d) 19, 27, 35, 43, 51 4.8 What does the following program do? 1 #include <stdio.h> 2 3 /* function main begins program execution */ 4 int main( void ) 5{ 6 int x; 7 int y; 8 int i; 9 int j; 10 11 /* prompt user for input */ 12 printf( "Enter two integers in the range 1-20: " ); 13 scanf( "%d%d", &x, &y ); /* read values for x and y */ 14 15 for ( i = 1; i <= y; i++ ) { /* count from 1 to y */ 16 17 for ( j = 1; j <= x; j++ ) { /* count from 1 to x */ 18 printf( "@" ); /* output @ */ 19 } /* end inner for */ 20 21 printf( "\n" ); /* begin new line */ 22 } /* end outer for */ 23 24 return 0; /* indicate program ended successfully */ 25 } /* end function main */ 4.26 (Calculating the Value of PI ) Calculate the value of PIfrom the infinite series Print a table that shows the value of approximated by one term of this series, by two terms, by three terms, and so on. How many terms of this series do you have to use before you first get 3.14? 3.141? 3.1415? 3.14159? 4.36 What does the following program segment do? 1 for ( i = 1; i <= 5; i++ ) { 2 for ( j = 1; j <= 3; j++ ) { 3 for ( k = 1; k <= 4; k++ ) 4 printf( "*" ); 5 printf( "\n" ); 6} 7 printf( "\n" ); 8}