Домашната от Console Input Output

Моите, решения на задачите от лекцията за вход и изход от конзолата без последната ( над нея още работя ).
Условията:
1. Write a program that reads 3 integer numbers from the console and prints their sum.
2. Write a program that reads the radius r of a circle and prints its perimeter and 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.
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.
5. Write a program that gets two numbers from the console and prints the greater of them. Don’t use if statements.
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).
7. Write a program that gets a number n and after that gets more n numbers and calculates and prints their 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.
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, …
10. Write a program to calculate the sum (with accuracy of 0.001): 1 + 1/2 – 1/3 + 1/4 – 1/5 + …

Ето и линк към място за свалянето на решенията ми:


Домашни от Operators, Expressions And Statements


В тази тема домашните стават с една идея по-трудно, особено последната 🙂
1. Write an expression that checks if given integer is odd or even.
2. Write a boolean expression that checks for given integer if it can be divided (without remainder) by 7 and 5 in the same time.
3. Write an expression that calculates rectangle’s area by given width and height.
4. Write an expression that checks for given integer if its third digit (right-to-left) is 7. E. g. 1732  true.
5. Write a boolean expression for finding if the bit 3 (counting from 0) of a given integer is 1 or 0.
6. Write an expression that checks if given point (x, y) is within a circle K(O, 5).
7. Write an expression that checks if given positive integer number n (n ≤ 100) is prime. E.g. 37 is prime.
8. Write an expression that calculates trapezoid’s area by given sides a and b and height h.
9. Write an expression that checks for given point (x, y) if it is within the circle K( (1,1), 3) and out of the rectangle R(top=1, left=-1, width=6, height=2).
10. Write a boolean expression that returns if the bit at position p (counting from 0) in a given integer number v has value of 1. Example: v=5; p=1  false
11. Write an expression that extracts from a given integer i the value of a given bit number b. Example: i=5; b=2  value=1.
12. We are given integer number n, value v (v=0 or 1) and a position p. Write a sequence of operators that modifies n to hold the value v at the position p from the binary representation of n.
Example: n = 5 (00000101), p=3, v=1  13 (00001101)
n = 5 (00000101), p=2, v=0  1 (00000001)
13. Write a program that exchanges bits 3, 4 and 5 with bits 24, 25 and 26 of given 32-bit unsigned integer.
14*. Write a program that exchanges bits {p, p+1, …, p+k-1) with bits {q, q+1, …, q+k-1} of given 32-bit unsigned integer.

Т.к. домашните стават все по-дълги и по-дълги реших този път да ги кача на отделен сайт, вместо да ги поствам направо тук.


Primitive Data Types and Variables


Това е втората домашна от C# – Part1 курса:

1.Declare five variables choosing for each of them the most appropriate of the types byte, sbyte, short, ushort, int, uint, long, ulong to represent the following values: 52130, -115, 4825932, 97, -10000.

using System;

class SimpleDeclaration
{
    static void Main()
    {
        ushort unsignedShort = 52130;
        sbyte simpleByte = -115;
        int intiger = 4825932;
        byte smallByte = 97;
        short simpleShort = -10000;
    }
}

2. Which of the following values can be assigned to a variable of type float and which to a variable of type double: 34.567839023, 12.345, 8923.1234857, 3456.091?

using System;

class DoublesAndFloats
{
    static void Main()
    {
        double firstDouble = 34.567839023;
        float firstFloat = 12.345f;
        double secondDouble = 8923.1234857;
        float secondFloat = 3456.091f;
    }
}

3.Write a program that safely compares floating-point numbers with precision of 0.000001. Examples: (5.3 ; 6.01)  false; (5.00000001 ; 5.00000003)  true

using System;

class FloatingPointComparison
{
    static void Main()
    {
        decimal firstNumber = 1235.6666666666666666666666666M;
        decimal secondNumber = 6.01M;
        decimal roundedOne = Math.Round(firstNumber, 6);
        Console.WriteLine(roundedOne);
        decimal roundedTwo = Math.Round(secondNumber, 6);
        if (roundedOne == roundedTwo)
        {
            Console.WriteLine("The numbers are equal!");
        }
        else if (roundedOne > roundedTwo)
        {
            Console.WriteLine("The first number is greater than the second!");
        }
        else
        {
            Console.WriteLine("The second number is greater than the first!");
        }
    }
}

4.Declare an integer variable and assign it with the value 254 in hexadecimal format. Use Windows Calculator to find its hexadecimal representation.

using System;

class HexValue
{
    static void Main()
    {
        int hexValue = 0xFE;
    }
}

5. Declare a character variable and assign it with the symbol that has Unicode code 72. Hint: first use the Windows Calculator to find the hexadecimal representation of 72.

using System;

class UniCodeSimbyl
{
    static void Main(string[] args)
    {
        char symbol = 'u0048';
        Console.WriteLine(symbol);
    }
}

6. Declare a boolean variable called isFemale and assign an appropriate value corresponding to your gender.

using System;

class Gender
{
    static void Main()
    {
        bool isFemale = false;
    }
}

7. Declare two string variables and assign them with “Hello” and “World”. Declare an object variable and assign it with the concatenation of the first two variables (mind adding an interval). Declare a third string variable and initialize it with the value of the object variable (you should perform type casting).

using System;

class ConcatenationAndCasting
{
    static void Main()
    {
        string firstPart = "Hello ";
        string secondPart = "World";
        object wholeObj = firstPart + secondPart;
        string wholeString = (string)wholeObj;
    }
}

8.Declare two string variables and assign them with following value: /The “use” of quotations causes difficulties./
Do the above in two different ways: with and without using quoted strings.

using System;

class QuoteProblems
{
    static void Main()
    {
        Console.WriteLine("The "use" of quotations causes difficulties.");
        Console.WriteLine(@"The ""use"" of quotations causes difficulties.");
    }
}

9.Write a program that prints an isosceles triangle of 9 copyright symbols ©. Use Windows Character Map to find the Unicode code of the © symbol. Note: the © symbol may be displayed incorrectly.

using System;
using System.Text;

class Program
{
    static void Main(string[] args)
    {
        Console.OutputEncoding = Encoding.Unicode;
        int copyRignt = 169;
        byte spaceing = 2;
        for (int rows = 0; rows < 3; rows++)
        {
            for (int spaceCounter = 0; spaceCounter < spaceing; spaceCounter++) 
            {
                Console.Write(" ");
            }
            for (int cols = 0; cols <= rows; cols++)
            {
                Console.Write((char)copyRignt);
            }
            for (int cols = 0; cols <= rows - 1; cols++)
            {
                Console.Write((char)copyRignt);
            }
            spaceing--;
            Console.WriteLine();
        }
    }
}

10. A marketing firm wants to keep record of its employees. Each record would have the following characteristics – first name, family name, age, gender (m or f), ID number, unique employee number (27560000 to 27569999). Declare the variables needed to keep the information for a single employee using appropriate data types and descriptive names.

using System;

class MarketingFirmInfo
{
    static void Main()
    {
        string firstName = "Ivan";
        string lastName = "Ivanov";
        byte age = 23;
        char gender = 'm';
        long ID = 213124123;
        uint uniqueNumber = 27569999;
    }
}

11. Declare two integer variables and assign them with 5 and 10 and after that exchange their values.

using System;

class ChangeingValues
{
    static void Main()
    {
        int a = 5;
        int b = 10;
        int c;
        c = a;
        a = b;
        b = c;
        Console.WriteLine("The value of " a " is {0} and the value of " b " is {1}", a, b);
    }
}

12. Find online more information about ASCII (American Standard Code for Information Interchange) and write a program that prints the entire ASCII table of characters on the console.

using System;
using System.Text;

class ASCIIsymbols
{
    static void Main()
    {
        Console.OutputEncoding = Encoding.Unicode;
        for (int i = 0; i <= 255; i++ )
        {
            char symbol = (char)i;
            Console.WriteLine("ASCII symbol {0} and its number {1}",symbol,i);
        }
    }
}

13. Create a program that assigns null values to an integer and to double variables. Try to print them on the console, try to add some values or the null literal to them and see the result.

using System;

class NullValues
{
    static void Main()
    {
        int? nullVauleOne = null;
        double? nullValueTwo = null;
        Console.WriteLine("First varibale {0} and second varibale {1}", nullVauleOne, nullValueTwo);
        Console.WriteLine(nullVauleOne + nullValueTwo);
        Console.WriteLine(nullVauleOne + 12);
        nullVauleOne = 5;
        Console.WriteLine(nullVauleOne.GetValueOrDefault());
    }
}

14. A bank account has a holder name (first name, middle name and last name), available amount of money (balance), bank name, IBAN, BIC code and 3 credit card numbers associated with the account. Declare the variables needed to keep the information for a single bank account using the appropriate data types and descriptive names.

using System;

class DoublesAndFloats
{
    static void Main()
    {
        string firstName = "Teodor";
        string middleName = "Ivanov";
        string lastName = "Kurtev";
        decimal balance = 1000;
        string bankName = "BNB";
        string IBAN = "";
        string BIC = "";
        ulong creditCard1 = 1111;
        ulong creditCard2 = 123123;
        ulong creditCard3 = 334324;

    }
}

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);
        
    }
}