Exercises

  1. 3.7 Fill in the blanks in each of the following statements:

    1.                 are used to document an app and improve its readability.

    2. A decision can be made in a C# app with a(n)                .

    3. Calculations are normally performed by                 statements.

    4. The arithmetic operators with the same precedence as multiplication are                 and                .

    5. When parentheses in an arithmetic expression are nested, the                 set of parentheses is evaluated first.

    6. A location in the computer’s memory that may contain different values at various times throughout the execution of an app is called a(n)                .

  2. 3.8 Write C# statements that accomplish each of the following tasks:

    1. Display the message "Enter an integer: ", leaving the cursor on the same line.

    2. Assign the product of variables b and c to variable a.

    3. State that an app performs a simple payroll calculation (i.e., use text that helps to document an app).

  3. 3.9 State whether each of the following is true or false. If false, explain why.

    1. C# operators are evaluated from left to right.

    2. The following are all valid variable names: _under_bar_, m928134, t5, j7, her_sales, his_account_total, a, b, c, z and z2.

    3. A valid C# arithmetic expression with no parentheses is evaluated from left to right.

    4. The following are all invalid variable names: 3g, 87, 67h2, h22 and 2h.

  4. 3.10 Assuming that x = 2 and y = 3, what does each of the following statements display?

    1. Console.WriteLine($"x = {x}");

    2. Console.WriteLine($"Value of {x} + {x} is {x + x}");

    3. Console.Write("x =");

    4. Console.WriteLine($"{x + y} = {y + x}");

  5. 3.11 Which of the following C# statements contain variables whose values are modified?

    1. p = i + j + k + 7;

    2. Console.WriteLine("variables whose values are modified");

    3. Console.WriteLine("a = 5");

    4. value = int.Parse(Console.ReadLine());

  6. 3.12 Given that y=ax3+7, which of the following are correct C# statements for this equation?

    1. y = a * x * x * x + 7;

    2. y = a * x * x * (x + 7);

    3. y = (a * x) * x * (x + 7);

    4. y = (a * x) * x * x + 7;

    5. y = a * (x * x * x) + 7;

    6. y = a * x * (x * x + 7);

  7. 3.13 (Order of Evaluation) State the order of evaluation of the operators in each of the following C# statements and show the value of x after each statement is performed:

    1. x = 7 + 3 * 6 / 2 - 1;

    2. x = 2 % 2 + 2 * 2 - 2 / 2;

    3. x = (3 * 9 * (3 + (9 * 3 / (3))));

  8. 3.14 (Displaying Numbers) Write an app that displays the numbers 1 to 4 on the same line, with each pair of adjacent numbers separated by one space. Write the app using the following techniques:

    1. Use one Console.WriteLine statement.

    2. Use four Console.Write statements.

    3. Use one Console.WriteLine statement with four int variables and string interpolation.

  9. 3.15 (Arithmetic) Write an app that asks the user to enter two integers, obtains them from the user and displays their sum, product, difference and quotient (division). Use the techniques shown in Fig. 3.14.

  10. 3.16 (Comparing Integers) Write an app that asks the user to enter two integers, obtains them from the user and displays the larger number followed by the words "is larger". If the numbers are equal, display the message "These numbers are equal." Use the techniques shown in Fig. 3.22.

  11. 3.17 (Arithmetic, Smallest and Largest) Write an app that inputs three integers from the user and displays the sum, average, product, and smallest and largest of the numbers. Use the techniques from Fig. 3.22. [Note: The average calculation in this exercise should result in an integer representation of the average. So, if the sum of the values is 7, the average should be 2, not 2.3333….]

  12. 3.18 (Displaying Shapes with Asterisks) Write an app that displays a box, an oval, an arrow and a diamond using asterisks (*), as in Fig. 3.25.

  13. 3.19 What does the following code display?

    Console.WriteLine("* ** *** **** *****");

  14. 3.20 What does the following code display?

    
    Console.WriteLine("*");
    Console.WriteLine("***");
    Console.WriteLine("*****");
    Console.WriteLine("****");
    Console.WriteLine("**");
    

    Fig. 3.25 Sample output for Exercise 3.18.

    Alternate View
    
    *********             ***             *                  *
    *       *         *         *        ***                * *
    *       *     *                 *   *****             *     *
    *       *     *                 *     *             *         *
    *       *     *                 *     *           *             *
    *       *     *                 *     *             *         *
    *       *     *                 *     *               *     *
    *       *         *         *         *                 * *
    *********             ***             *                  *
    
  15. 3.21 What does the following code display?

    
    Console.Write("*");
    Console.Write("***");
    Console.Write("*****");
    Console.Write("****");
    Console.WriteLine("**");
    
  16. 3.22 What does the following code display?

    
    Console.Write("*");
    Console.WriteLine("***");
    Console.WriteLine("*****");
    Console.Write("****");
    Console.WriteLine("**");
    
  17. 3.23 What does the following code display?

    
    string s1 = "*";
    string s2 = "***";
    string s3 = "*****";
    Console.WriteLine($"{s1}
    {s2}
    {s3}");
    
  18. 3.24 (Odd or Even) Write an app that reads an integer, then determines and displays whether it’s odd or even. [Hint: Use the remainder operator. An even number is a multiple of 2. Any multiple of 2 leaves a remainder of 0 when divided by 2.]

  19. 3.25 (Multiples) Write an app that reads two integers, determines whether the first is a multiple of the second and displays the result. [Hint: Use the remainder operator.]

  20. 3.26 (Diameter, Circumference and Area of a Circle) Here’s a peek ahead. In this chapter, you have learned about integers and the type int. C# also can represent floating-point numbers that contain decimal points, such as 3.14159. Write an app that inputs from the user the radius of a circle as an integer and displays the circle’s diameter, circumference and area using the floating-point value 3.14159 for π. Use the techniques shown in Fig. 3.14. [Note: You may also use the predefined constant Math.PI for the value of π. This constant is more precise than the value 3.14159. Class Math is defined in namespace System]. Use the following formulas (r is the radius):

    diameter=2rcircumference=2πrarea=πr2

    Don’t store each calculation’s result in a variable. Rather, specify each calculation as the value to be output in a Console.WriteLine statement. The values produced by the circumference and area calculations are floating-point numbers. You’ll learn more about these in Chapter 5.

  21. 3.27 (Integer Equivalent of a Character) Here’s another peek ahead. In this chapter, you have learned about integers and the type int. C# also can represent uppercase letters, lowercase letters and a considerable variety of special symbols. Every character has a corresponding integer representation. The set of characters a computer uses and the corresponding integer representations for those characters is called that computer’s character set. You can indicate a character value in an app simply by enclosing that character in single quotes, as in 'A'.

    You can determine the integer equivalent of a character by preceding that character with (int), as in

    
    (int) 'A'
    

    The keyword int in parentheses is known as a cast operator, and the entire expression is called a cast expression. (You’ll learn about cast operators in Chapter 5.) The following statement outputs a character and its integer equivalent:

    
    Console.WriteLine($"The character {'A'} has the value {(int)'A'}");
    

    When the preceding statement executes, it displays the character A and the value 65 as part of the string. See Appendix C for a list of characters and their integer equivalents.

    Using statements similar to the one shown earlier in this exercise, write an app that displays the integer equivalents of some uppercase letters, lowercase letters, digits and special symbols. Display the integer equivalents of the following: A B C a b c 0 1 2 $ * + / and the space character.

  22. 3.28 (Digits of an Integer) Write an app that inputs one number consisting of five digits from the user, separates the number into its individual digits and displays the digits separated from one another by three spaces each. For example, if the user types 42339, the app should display

    
    4 2 3 3 9
    

    Assume that the user enters the correct number of digits. What happens when you execute the app and type a number with more than five digits? What happens when you execute the app and type a number with fewer than five digits? [Hint: It’s possible to do this exercise with the techniques you learned in this chapter. You’ll need to use both division and remainder operations to “pick off” each digit.]

  23. 3.29 (Table of Squares and Cubes) Using only the programming techniques you learned in this chapter, write an app that calculates the squares and cubes of the numbers from 0 to 10 and displays the resulting values in table format, as shown in. All calculations should be done in terms of a variable x. [Note: This app does not require any input from the user.]

    Fig. 3.26 Sample output for Exercise 3.29.

    Alternate View
    
    number     square     cube
    0          0          0
    1          1          1
    2          4          8
    3          9          27
    4          16         64
    5          25         125
    6          36         216
    7          49         343
    8          64         512
    9          81         729
    10         100        1000
    
  24. 3.30 (Counting Negative, Positive and Zero Values) Write an app that inputs five numbers and determines and displays the number of negative numbers input, the number of positive numbers input and the number of zeros input.

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

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