C# – Types Of Data And Variables

Условия и решения на задачите от лекция “Types Of Data And Variables”

Задача 1. Declare Five Variables

Условие: 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;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _1.DeclareFiveVariables
{
    class Program
    {
        static void Main()
        {
            ushort number1 = 52130;
            sbyte number2 = -115;
            int number3 = 4825923;
            byte number4 = 97;
            short number5 = -10000;
            Console.WriteLine("{0};\n{1};\n{2};\n{3};\n{4};",number1,number2,number3,number4,number5);
        }
    }
}

Задача 2. Assignment To Float And Double

Условие: 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;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _2.AssignmentToFloatAndDouble
{
    class Program
    {
        static void Main()
        {
            double number1 = 34.567839023;
            float number2 = 12.345f;//But it can be also double
            double number3 = 8923.1234857;
            float number4 = 3456.091f;//But it can be also double
            Console.WriteLine("{0};\n{1};\n{2};\n{3};", number1, number2, number3, number4);

        }
    }
}

Задача 3. Comparison Of Floating Point Numbers

Условие: 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;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _3.ComparisonOfFloatingPointNumbers
{
    class Program
    {
        static void Main()
        {
            bool result = false;
            //Console.WriteLine("\u2192");
            Console.WriteLine("Enter the first number:");
            double number1 = double.Parse(Console.ReadLine());
            //string sNumber1 = Convert.ToString(number1);

            Console.WriteLine("Enter the second number:");
            double number2 = double.Parse(Console.ReadLine());

            double deduction = number1 - number2;
            deduction = Math.Abs(deduction);
            Console.WriteLine(deduction);
            if (deduction < 0.000001)
            {
                result = true;
            }
            Console.WriteLine("({0};{1})\u2192{2}",number1,number2,result);
        }
    }
}

Задача 4. Decimal To Hexadecimal Conversion

Условие: Declare an integer variable and assign it with the value 254 in hexadecimal format. Use Windows Calculator to find its hexadecimal representation.

Решение:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _4.DecimalToHexadecimalConversion
{
    class Program
    {
        static void Main()
        {
            int number = 0xFE;
            Console.WriteLine(number);
        }
    }
}

Задача 5. Unicode Assignment Of Character

Условие: 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;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _5.UnicodeAssignmentOfCharacter
{
    class Program
    {
        static void Main()
        {
            char character = '\u0048';
            Console.WriteLine(character);
        }
    }
}

Задача 6. Is Female

Условие: Declare a boolean variable called isFemale and assign an appropriate value corresponding to your gender.

Решение:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _6.IsFemale
{
    class Program
    {
        static void Main()
        {
            bool IsFemale = false;
            Console.WriteLine(IsFemale);
        }
    }
}

Задача 7. String Concatenation

Условие: 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;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _7.StringConcatenation
{
    class Program
    {
        static void Main(string[] args)
        {
            string string1 = "Hello";
            string string2 = "World";
            object concatenatedString = string1 + " " + string2;
            string newString = (string)concatenatedString;
            Console.WriteLine(newString);
        }
    }
}

Задача 8. Using Of Quotations In Strings

Условие: Declare two string variables and assign them with following value:
Variables_1
Do the above in two different ways: with and without using quoted strings.

Решение:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _8.UsingOfQuotationsInStrings
{
    class Program
    {
        static void Main()
        {
            string string2 = "The \"use\" of quotations causes difficulties";
            string string1=@"The ""use"" of quotations causes difficulties";
            Console.WriteLine("{0}\n{1}",string1,string2);
        }
    }
}

Задача 9. Isosceles Triangle Of 9 Symbols

Условие: 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.Collections.Generic;
using System.Linq;
using System.Text;

namespace _9.IsoscelesTriangleOf9Symbols
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("   \u00A9   ");
            Console.WriteLine("  \u00A9 \u00A9  ");
            Console.WriteLine(" \u00A9   \u00A9 ");
            Console.WriteLine("\u00A9 \u00A9 \u00A9 \u00A9");
        }
    }
}

Задача 10. Marketing Firm Data Record

Условие: 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;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _10.MarketingFirmDataRecord
{
    class Program
    {
        static void Main()
        {
            string firstName;
            string familyName;
            byte age;
            char gender;
            string IDnumber;
            uint uniqueEmployeeNumber;

            firstName = "Petar";
            familyName = "Yankov";
            age = 31;
            gender = 'm';
            IDnumber = "8112312891";
            uniqueEmployeeNumber = 27560001;
            Console.WriteLine("{0} {1}\n{2}\n{3}\n{4}\n{5}",firstName,familyName,age,gender,IDnumber,uniqueEmployeeNumber);
        }
    }
}

Задача 11. Two Intiger Values Exchange

Условие: Declare two integer variables and assign them with 5 and 10 and after that exchange their values.

Решение:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _11.TwoIntigerValuesExchange
{
    class Program
    {
        static void Main()
        {
            int number1 = 5;
            int number2 = 10;
            Console.WriteLine("Before exchange \u2192 number1={0}\tnumber2={1}", number1, number2);
            int number3;
            number3 = number1;
            number1 = number2;
            number2 = number3;
            Console.WriteLine("After exchange \u2192 number1={0}\tnumber2={1}", number1, number2);
        }
    }
}

Задача 12. ASCII Table

Условие: Declare two integer variables and assign them with 5 and 10 and after that exchange their values.

Решение:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _12.ASCIITable
{
    class Program
    {
        static void Main()
        {
            for (int i = 0; i < 128; i++)
            {
                Console.WriteLine((char)i);
            }
        }
    }
}

Задача 13. Null Values Assignment

Условие: 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;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _13.NullValuesAssignment
{
    class Program
    {
        static void Main()
        {
            int? number1 = null;
            double? number2 = null;
            Console.WriteLine("{0};{1}", number1, number2);

            number1 = number1 + null;
            number2 = number2 + null;

            Console.WriteLine("{0};{1}", number1, number2);

            number1 = number1 + 100;
            number2 = number2 + 100.5;

            Console.WriteLine("{0};{1}", number1, number2);

            number1 = number1 + 100;
            number2 = number2 + 100.5;

            Console.WriteLine("{0};{1}", number1.GetValueOrDefault(), number2.GetValueOrDefault());
        }
    }
}

Задача 14. Bank Account

Условие: 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;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _14.BankAccount
{
    class Program
    {
        static void Main()
        {
            string holderFirstName;
            string holderMiddleName;
            string holderLastName;
            decimal balance;
            string bankName;
            string IBAN;
            string BIC_Code;
            long creditCardNumber1;
            long creditCardNumber2;
            long creditCardNumber3;

            holderFirstName = "Petar";
            holderMiddleName = "Stoyanov";
            holderLastName = "Yankov";
            balance = 5.77M;
            bankName = "DSK";
            IBAN = "BG85STSA12345678912345";
            BIC_Code = "STSABGSF";
            creditCardNumber1 = 6776030078884332;
            creditCardNumber2 = 1293129091298001;
            creditCardNumber3 = 1209839102309111;

            Console.WriteLine("{0} {1} {2}\n{3}\n{4}\n{5}\n{6}\n{7}\n{8}\n{9}",holderFirstName,holderMiddleName,holderLastName,balance,bankName,IBAN,BIC_Code,creditCardNumber1,creditCardNumber2,creditCardNumber3);

        }
    }
}

Оставете коментар

Вашият имейл адрес няма да бъде публикуван. Задължителните полета са отбелязани с *