C# For Dummies

Chapter 9. Methods

1. Write a method that by given name prints on the console "Hello, !" (for example: "Hello, Peter!" ).

Guidelines: Use a method that takes the name as parameter of type string.

Solution:
class Program
{
    static void ReturnName(string name)
    {
        Console.WriteLine("Hello {0}", name);
    }
    
    static void Main(string[] args)
    {
        Console.Write("Enter name: ");
        ReturnName(Console.ReadLine());
    }
}

2. Create a method GetMax() with two integer (int) parameters, that returns maximal of the two numbers. Write a program that reads three numbers from the console and prints the biggest of them. Use the GetMax() method you just created.

Guidelines: Use the expression Max(a, b, c) = Max(Max(a, b), c).

Solution:
Link
3. Write a method that returns the English name of the last digit of a given number. Example: for 512 prints "two"; for 1024 prints "four"

Guidelines: Use the reminder of division by 10 and then a switch statement.

Solution:
Link
4. Write a method that finds how many times certain number can be found in a given array.

Guidelines: The method must take as parameter an array of integer numbers (int[]) and the number that has to be counted (int).

Solution:
Link
5. Write a method that checks whether an element, from a certain position in an array is greater than its two neighbours.

Guidelines: Just perform a check. The elements of the first and the last position in the array will be compared only with their left and right neighbor.

Solution:
Link
6. Write a method that returns the position of the first occurrence of an element from an array, such that it is greater than its two neighbors simultaneously. Otherwise the result must be -1.

Guidelines: Invoke the method from the previous problem in a for-loop.

Solution:
Link
7. Write a method that prints the digits of a given decimal number in a reversed order. For example 256, must be printed as 652.

Guidelines: There are two solutions:
First solution: Let the number is num. So while num ≠ 0 we print its last digit (num % 10) and then divide num by 10.
Second solution: Convert the number into a string string and print it in a reverse order with a for-loop. This is a bit cheater’s approach.

Solution:
Link
8. Write a method that calculates the sum of two very long positive integer numbers. The numbers are represented as array digits and the last digit (the ones) is stored in the array at index 0. Make the method work for all numbers with length up to 10,000 digits.

Guidelines: The reader must implement own method that calculates the sum of very big numbers. The digits on position zero will keep the ones; the digit on the first position will keep the tenths and so on. When two very big numbers are about to be calculated, the ones of their sum will be equal to (firstNumber[0] + secondNumber[0]) % 10, the tenths on other side will be equal to (firstNumber[1] + secondNumber[1]) % 10 + (firstNumber[0] + secondNumber[0])/10 and so on.

Solution:
Link
9. Write a method that finds the biggest element of an array. Use that method to implement sorting in descending order.

Guidelines: First write a method that finds the biggest element in array and then modify it to find the biggest element in given range of the array, e.g. in the elements at indexes [3…10]. Finally find the biggest number in the range [1…n-1] and swap it with the first element, then find the biggest element in the range [2…n-1] and swap it with the second element of the array and so on. Think when the algorithm should finish.

Solution:
Link
10. Write a program that calculates and prints the n! for any n in the range [1…100] .

Guidelines: The reader must implement own method that calculates the product of very big numbers, because the value of 100! does not fit in variable of type ulong or decimal. The numbers can be represented in an array of reversed digits (one digit in each element). For example, the number 512 can be represented as {2, 1, 5}. Then the multiplication can be implemented in the way done in the elementary school (multiply digit by digit and then calculate the sum). Another easier way to work with extremely large numbers such as 100! is by using the library System.Numerics.dll (you have to add a reference to it in your project). Look for Information in internet about how to use the class System.Numerics.BigInteger. Finally calculate in a loop k! for k = 1, 2, …, n.

Solution:
Link
11. Write a program that solves the following tasks:
- Put the digits from an integer number into a reversed order.
- Calculate the average of given sequence of numbers.
- Solve the linear equation a * x + b = 0.
Create appropriate methods for each of the above tasks.
Make the program show a text menu to the user. By choosing an option of that menu, the user will be able to choose which task to be invoked.
Perform validation of the input data:
- The integer number must be a positive in the range [1…50,000,000].
- The sequence of numbers cannot be empty.
- The coefficient a must be non-zero.

Guidelines: Firstly, create the necessary methods. To create the menu display a list in which the actions are represented as numbers (1 – reverse, 2 – average, 3 – equation). Ask the user to choose from 1 to 3.

Solution:
Link

← Back