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;

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