Practicing and exploring

Test your knowledge and understanding by answering some questions, get some hands-on practice, and explore this chapter's topics with deeper research.

Exercise 6.1 - test your knowledge

Answer the following questions:

  1. What are the four access modifiers and what do they do?
  2. What is the difference between the static, const, and readonly keywords?
  3. How many parameters can a method have?
  4. What does a constructor do?
  5. Why do you need to apply the [Flags] attribute to an enum keyword when you want to store combined values?
  6. Why is the partial keyword useful?

Exercise 6.2 - practice writing mathematical methods

Create a console application named Ch06_Exercise02 and add three static methods to the Program class to perform the following tasks:

  • Numbers used to count are called "cardinal" numbers, for example, 1, 2, 3. Numbers used to order are "ordinal" numbers, for example, 1st, 2nd, 3rd. Write a method named CardinalToOrdinal that converts a cardinal int into an ordinal string, for example, it converts 1 into 1st, 2 into 2nd, and so on.
  • The factorial of 5 is 120, because factorials are calculated by multiplying the number by one less than itself and so on, like this: 5 x 4 x 3 x 2 x 1 = 120. The factorial of 3 is 6 because it is 3 x 2 x 1 = 6. Write a method named Factorial that calculates the factorial for an int variable passed to it as a parameter. You could either use a loop or a technique called recursion, which means a method that calls itself.
  • Prime factors are the combination of the smallest prime numbers, that, when multiplied together, will produce the original number. For example, the prime factors of 30 are 2 x 3 x 5. The prime factors of 4 are 2 x 2. Write a method named PrimeFactors that, when passed an int variable as a parameter, returns a string showing the prime factors as stated earlier.

In the Main method, prompt the user to press A, B, or C to choose between the three mathematical functions. Then, prompt the user to enter a number as input and then show the output.

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

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