Answers to Self-Review Exercises

  1. 3.1 a) left brace ({), right brace (}). b) semicolon (;). c) if. d) //. e) Blank lines, space characters, tab characters. f) Keywords. g) Main. h) Console.WriteLine and Console.Write. i) string interpolation.

  2. 3.2 The answers to Self-Review Exercise 3.2 are as follows:

    1. False. Comments do not cause any action to be performed when the app executes. They’re used to document apps and improve their readability.

    2. False. C# is case sensitive, so these variables are distinct.

    3. False. The remainder operator also can be used with noninteger operands in C#.

    4. False. The operators *, / and % are on the same level of precedence, and the operators + and - are on a lower level of precedence.

    5. True.

  3. 3.3 The answers to Self-Review Exercise 3.3 are as follows:

    1. 
      int c;
      int thisIsAVariable;
      int q76354;
      int number;
      
    2. Console.Write("Enter an integer: ");

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

    4. 
      if (number != 7)
      {
         Console.WriteLine("The variable number is not equal to 7");
      }
      
    5. Console.WriteLine("This is a C# app");

    6. Console.WriteLine("This is a C# app");

    7. Console.WriteLine($"The sum of {x} and {y} is {x + y}");

  4. 3.4 The answers to Self-Review Exercise 3.4 are as follows:

    1. Error: Semicolon after the right parenthesis of the condition (c < 7) in the if statement.

      Correction: Remove the semicolon after the right parenthesis. [Note: With the semicolon, the output statement executes regardless of whether the condition in the if is true.]

    2. Error: The relational operator => is incorrect.

      Correction: Change => to >=.

  5. 3.5 The answers to Self-Review Exercise 3.5 are as follows:

    1. // Calculating the product of three integers

    2. 
      int x;
      int y;
      int z;
      int result;
      
    3. Console.Write("Enter first integer: ");

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

    5. Console.Write("Enter second integer: ");

    6. y = int.Parse(Console.ReadLine());

    7. Console.Write("Enter third integer: ");

    8. z = int.Parse(Console.ReadLine());

    9. result = x * y * z;

    10. Console.WriteLine($"Product is {result}");

  6. 3.6 The solution to Self-Review Exercise 3.6 is as follows:

    Fig. 3.24 Solution to Exercise 3.6.

    Alternate View
    
      1   // Exercise 3.6: Product.cs
      2   // Calculating the product of three integers.
      3   using System;
      4
      5   class Product
      6   {
      7      static void Main()
      8      {
      9         int x; // stores first number to be entered by user
     10         int y; // stores second number to be entered by user
     11         int z; // stores third number to be entered by user
     12         int result; // product of numbers
     13
     14         Console.Write("Enter first integer: "); // prompt for input
     15         x = int.Parse(Console.ReadLine()); // read first integer
     16
     17         Console.Write("Enter second integer: "); // prompt for input
     18         y = int.Parse(Console.ReadLine()); // read second integer
     19
     20         Console.Write("Enter third integer: "); // prompt for input
     21         z = int.Parse(Console.ReadLine()); // read third integer
     22
     23         result = x * y * z; // calculate the product of the numbers
     24         Console.WriteLine($"Product is {result}");
     25      } // end Main
     26   } // end class Product
    
    
    Enter first integer: 10
    Enter second integer: 20
    Enter third integer: 30
    Product is 6000
    
..................Content has been hidden....................

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