четвъртък, 3 януари 2013 г.

C#: Console Input Output

Вход и изход от конзолата е една от най-лесните, кратки и приятни лекции до сега в курса по C#. Задачите от домашното към лекцията са сравнително лесни. Видео и материали от лекцията може да се намерят тук.

1. Write a program that reads 3 integer numbers from the console and prints their sum.
using System;

class ReadThreeNumbers
{
    static void Main()
    {
        Console.WriteLine("Enter the first number: ");
        int firstNumber = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter the second number: ");
        int secondNumber = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter the third number: ");
        int thirdNumber = int.Parse(Console.ReadLine());
        int sum = firstNumber + secondNumber + thirdNumber;
        Console.WriteLine("The sum is equal to {0}: ", sum);
    }
}

2. Write a program that reads the radius r of a circle and prints its perimeter and area.
using System;

class ReadRadiusOfCircle
{
    static void Main()
    {
        Console.WriteLine("Enter the radius of the circle: ");
        double radius = double.Parse(Console.ReadLine());
        double area = Math.PI * radius * radius;
        double perimeter = 2 * Math.PI * radius;
        Console.WriteLine("Perimeter is equal to: {0}", perimeter);
        Console.WriteLine("Area is equal to: {0}", area);
    }
}

3. A company has name, address, phone number, fax number, web site and manager. The manager has first name, last name, age and a phone number. Write a program that reads the information about a company and its manager and prints them on the console.
using System;

class CompanyInformation
{
    static void Main()
    {
        Console.WriteLine("Enter the name of the company:");
        string companyName = Console.ReadLine();
        Console.WriteLine("Enter the address of the company:");
        string address = Console.ReadLine();
        Console.WriteLine("Enter the phone Number: ");
        string phoneNumber = Console.ReadLine();
        Console.WriteLine("Enter the fax number:");
        string faxNumber = Console.ReadLine();
        Console.WriteLine("Enter the web site Address: ");
        string webSite = Console.ReadLine();
        Console.WriteLine("Enter the first name of the manager:");
        string managerFirstName = Console.ReadLine();
        Console.WriteLine("Enter the last name of the manager: ");
        string managerLastName = Console.ReadLine();
        string managerFullName = managerFirstName + " " + managerLastName;
        Console.WriteLine("Enter the age of the manager:");
        byte age = byte.Parse(Console.ReadLine());
        Console.WriteLine("Enter the phone number of the manager:");
        string managerPhoneNumber = Console.ReadLine();
        Console.WriteLine("Company Information");
        Console.WriteLine("Companu name: {0}", companyName);
        Console.WriteLine("Company Address: {0}", address);
        Console.WriteLine("Company phone number: {0}", phoneNumber);
        Console.WriteLine("Company fax number: {0}", faxNumber);
        Console.WriteLine("Company Web site: {0}", webSite);
        Console.WriteLine("Company manager: {0}", managerFullName);
        Console.WriteLine("Manager information");
        Console.WriteLine("Manager first name: {0}", managerFirstName);
        Console.WriteLine("Manager last name: {0}", managerLastName);
        Console.WriteLine("Manager age {0}", age);
        Console.WriteLine("Manager phone number: {0}", managerPhoneNumber);
    }
}

4. Write a program that reads two positive integer numbers and prints how many numbers p exist between them such that the reminder of the division by 5 is 0 (inclusive). Example: p(17,25) = 2.
using System;

class BetweenTwoNumbers
{
    static void Main()
    {
        Console.WriteLine("Enter the first number:");
        int firstNumber = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter the second number:");
        int secondNumber = int.Parse(Console.ReadLine());
        int firstNumberDivide = firstNumber / 5;
        int secondNumberDivide = secondNumber / 5;
        int difference = Math.Max(firstNumberDivide, secondNumberDivide) - Math.Min(firstNumberDivide, secondNumberDivide);
        Console.WriteLine("P({0},{1})={2}", firstNumber, secondNumber, difference);
    }
}

5. Write a program that gets two numbers from the console and prints the greater of them. Don’t use if statements.
using System;

class GreaterNumber
{
    static void Main()
    {
        Console.WriteLine("Enter the first number:");
        int firstNumber = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter the second number:");
        int secondNumber = int.Parse(Console.ReadLine());
        int greaterNumber = Math.Max(firstNumber, secondNumber);
        Console.WriteLine("Greater number is: {0}", greaterNumber);
    }
}

6. Write a program that reads the coefficients a, b and c of a quadratic equation ax2+bx+c=0 and solves it (prints its real roots).
using System;

class QuadraticEquation
{
    static void Main()
    {
        Console.WriteLine("Enter value for 'a'");
        double a = double.Parse(Console.ReadLine());
        Console.WriteLine("Enter value for 'b'");
        double b = double.Parse(Console.ReadLine());
        Console.WriteLine("Enter value for 'c'");
        double c = double.Parse(Console.ReadLine());
        double d = b * b - (4 * a * c);
        Console.WriteLine("d = {0}", d);
        if (d < 0)
        {
            Console.WriteLine("There are no real roots");
        }
        else if (d == 0)
        {
            double x = (-1 * b) / (2 * a);
            Console.WriteLine("x = {0}" , x);
        }
        else
        {
            double x1 = ((-1 * b) + Math.Sqrt(d)) / (2 * a);
            double x2 = ((-1 * b) - Math.Sqrt(d)) / (2 * a);
            Console.WriteLine("x1 = {0}" , x1);
            Console.WriteLine("x2= {0}" , x2);
        }
    }
}

7. Write a program that gets a number n and after that gets more n numbers and calculates and prints their sum.
using System;

class GetANumber
{
    static void Main()
    {
        Console.WriteLine("Enter one number:");
        int number = int.Parse(Console.ReadLine());
        int sum = 0;
        for (int i = 0; i < number; i++)
        {
            Console.WriteLine("Enter a value:");
            sum += int.Parse(Console.ReadLine());

        }
        Console.WriteLine("The sum is: {0}", sum);
    }
}

8. Write a program that reads an integer number n from the console and prints all the numbers in the interval [1..n], each on a single line.
using System;

class PrintNumbersInInterval
{
    static void Main()
    {
        Console.WriteLine("Enter a number");
        int n = int.Parse(Console.ReadLine());
        for (int i = 1; i <= n; i++)
        {
            Console.WriteLine(i);
        }
    }
}

9. Write a program to print the first 100 members of the sequence of Fibonacci: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, …
using System;

class FibonacciSequence
{
    static void Main()
    {
        int number1 = 0;
        int number2 = 1;
        int sum = 1;
        while (number1 < 600)
        {
            sum = number1 + number2;
            number1 = number2;
            number2 = sum;
            Console.WriteLine(number2);
        }
    }
}

10. Write a program to calculate the sum (with accuracy of 0.001): 1 + 1/2 - 1/3 + 1/4 - 1/5 + ...
using System;

class CalculateSum
{
    static void Main()
    {
        decimal sum = 1m;
        decimal previousSum = 1m;
        decimal counter = 2m;
        do
        {
            previousSum = sum;
            if (counter % 2 == 0)
            {
                sum += (decimal)1 / counter;
            }
            else
            {
                sum -= (decimal)1 / counter;
            }
            counter++;
        } while (Math.Abs(sum - previousSum) >= (decimal)0.001m);
        Console.WriteLine("{0:0.000}", sum);
    }
}

Няма коментари:

Публикуване на коментар