C# For Dummies

Chapter 1. Introduction to Programming

1. Install and make yourself familiar with Microsoft Visual Studio and Microsoft Developer Network (MSDN) Library Documentation.

Guidelines: If you have a DreamSpark account (www.dreamspark.com), or your school or university offers free access to Microsoft products, install the full version of Microsoft Visual Studio. If you do not have the opportunity to work with the full version of Microsoft Visual Studio, you can download Visual Studio Express for free from the Microsoft web site; it is completely free and works well for learning purposes.

Solution: A free version of Visual Studio can be downloaded from the following link: Visual Studio

2. Find the description of the System.Console class in the standard .NET API documentation (MSDN Library).

Guidelines: A Google search will often be the fastest way to find documentation for a given .NET class.

Solution: Description of the following class can be found at: Console Class

3. Find the description of the System.Console.WriteLine() method and its different possible parameters in the MSDN Library.

Guidelines: Use the same approach as from the previous exercise.

Solution: Description of the following class can be found at: Console.WriteLine Method

4. Compile and execute the sample program from this chapter using the command prompt (the console) and Visual Studio.

Guidelines: We create a file named HelloCSharp.cs, save the code in the program, compile HelloCSharp.cs to HelloCSharp.exe and execute HelloCSharp.exe

Solution:
class HelloCSharp
{
    static void Main()
    {
        System.Console.WriteLine("Hello C#!");
    }
}

5. Modify the sample program to print a different greeting, for example "Good Day!".

Guidelines: Modify the program from the last solution to print a different greeting, for example “Good Day!”.

Solution:
class HelloCSharp
{
    static void Main()
    {
        System.Console.WriteLine("Добър ден");
    }
}

6. Write a console application that prints your first and last name on the console.

Guidelines: Find how to use the System.Console.Write() method.

Solution:
class HelloCSharp
{
    static void Main()
    {
        System.Console.WriteLine("Ivan Popov");
    }
}

7. Write a program that prints the following numbers on the console 1, 101, 1001, each on a new line.

Guidelines: Use the System.Console.WriteLine() method.

Solution:
class HelloCSharp
{
    static void Main()
    {
        System.Console.WriteLine("1");
        System.Console.WriteLine("101");
        System.Console.WriteLine("1001");
    }
}

8. Write a program that prints on the console the current date and time.

Guidelines: Find out what features are offered by the System.Math class.

Solution:
class HelloCSharp
{
    static void Main()
    {
        System.Console.WriteLine(System.DateTime.Now);
    }
}

9. Write a program that prints the square root of 12345.

Guidelines: Find out what features are offered by the System.DateTime class.

Solution:
class HelloCSharp
{
    static void Main()
    {
        System.Console.WriteLine(Math.Sqrt(12345));
    }
}

10. Write a program that prints the first 100 members of the sequence 2, -3, 4, -5, 6, -7, 8.

Guidelines: Try to learn on your own how to use loops in C#.

Solution:
class HelloCSharp
{
    static void Main()
    {
        for (int i = 2; i < 103; i++)
        {
            if (i % 2 == 0) Console.WriteLine(i);
            else Console.Write("-{0}", i);
        }
    }
}

11. Write a program that reads your age from the console and prints your age after 10 years.

Guidelines: Use the methods System.Console.ReadLine() and int.Parse()

Solution:

class HelloCSharp
{
    static void Main()
    {
        Console.Write("Enter your age: ");
        int age = int.Parse(Console.ReadLine());
        Console.WriteLine("Your age after 10 years will be {0}", age + 10);
    }
}

← Back