Introduction to Programming


В следващите няколко реда ще публикувам примерни решения на задачите от Лекция Първа на Телерик Академи.

2. Create, compile and run a “Hello C#” console application.

using System;

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

3. Modify the application to print your name.

using System;

class HelloMyName
{
    static void Main()
    {
        Console.WriteLine("Hello, Teodor!");
    }
}

4. Write a program to print the numbers 1, 101 and 1001.

using System;

class PrintNumbers
{
    static void Main()
    {
        Console.WriteLine(1);
        Console.WriteLine(101);
        Console.WriteLine(1001);
    }
}

6. Create console application that prints your first and last name.

using System;

class PrintFirstAndLast
{
    static void Main()
    {
        Console.WriteLine("Teodor Kurtev");
    }
}

7. Create a console application that prints the current date and time.

using System;

class TimeAndDate
{
    static void Main()
    {
        Console.WriteLine("The date and time are:");
        Console.WriteLine(DateTime.Now);
    }
}

8. Create a console application that calculates and prints the square of the number 12345.

class Square
using System;

class Square
{
    static void Main()
    {
        Console.WriteLine("The square of 12345 is:");
        Console.WriteLine(Math.Pow(12345,2));
        // Another possibility is simply multiplying the 12345 and 12345
    }
}

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

using System;

class Program
{
    static void Main()
    {
        for (int i = 2; i < 12; i++)
        {
            if (i % 2 == 0)
            {
                Console.WriteLine(i);
            }
            else
            {
                Console.WriteLine(-i);
            }
        }
    }
}

Друго решение е това, макар че при него се проявява проблем с последния елемент

using System;

class NumberSequence
{
    static void Main()
    {
        Console.WriteLine("The first 10 numbers of the sequence are:");
        for (int i = 2; i < 12;i = i + 2 )
        {
            Console.Write(" " + i);
            Console.Write(" " + - (i + 1));
        }
        Console.WriteLine();
    }
}

12. * Write a program to read your age from the console and print how old you will be after 10 years.

using System;

class FutureAge
{
    static void Main()
    {    
        int yearBorn;
        while (true)
        {
            Console.WriteLine("Please enter the year you were born:");
            if ( int.TryParse(Console.ReadLine(), out yearBorn) && yearBorn > 1900 && yearBorn < DateTime.Now.Year)
            {
                break;
            }
            else
            {
                Console.WriteLine("Incorect data, please try again!");
            }
        }
        
        int yearsNow = DateTime.Now.Year - yearBorn;
        int futureAge = yearsNow + 10;
        Console.WriteLine("Your age now is {0} and you will be {1} years old after 10 years", yearsNow, futureAge);
        
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s