Advertisement
wingman007

2018_Programming_C#_BIT_1kurs_PartTime_Lection3

Dec 13th, 2018
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.43 KB | None | 0 0
  1. using System;
  2. using System.Globalization;
  3. using System.Text;
  4. using System.Threading;
  5.  
  6. namespace HelloKristina
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             Console.WriteLine("Hello World!");
  13.             // Primitive data types
  14.             byte myByte = 255;
  15.             sbyte mySByte = -128;
  16.  
  17.             short myShort = 123;// -2^15 2^15 - 1
  18.             ushort myUshort = 65535;
  19.  
  20.             long myLong = -3424523;
  21.             ulong myUlong = 45435;
  22.  
  23.             int age1 = 55;
  24.             int age2 = 18;
  25.             int ageResult;
  26.             ageResult = age1 + age2;
  27.  
  28.             float myFloat = 3.12345645455353453f;
  29.             double myDouble = 12.4353454354353453454353453453453454;
  30.  
  31.             decimal decimalPI = 3.14159265358979323846m;
  32.             Console.WriteLine(decimalPI); // 3.14159265358979323846
  33.  
  34.             Console.WriteLine(myFloat);
  35.             Console.WriteLine(myDouble);
  36.  
  37.  
  38.  
  39.             char myChar = 'P';
  40.             myChar = '\u0041'; // myChar = 'A';
  41.  
  42.             string myString = "Petya";
  43.  
  44.             bool myBool = true; // false | true
  45.  
  46.             // Object myObject = 3;
  47.  
  48.             Console.WriteLine(myChar);
  49.  
  50.             Console.WriteLine(ageResult);
  51.  
  52.             // Operators
  53.             int a = 11;
  54.             int b = 2;
  55.  
  56.             // Arithmetic operators
  57.             Console.WriteLine(a + b);
  58.             Console.WriteLine(a - b);
  59.             Console.WriteLine(a / b);
  60.             Console.WriteLine(a * b);
  61.             Console.WriteLine(a++);
  62.             Console.WriteLine(a);
  63.             Console.WriteLine(++a);
  64.             Console.WriteLine(a % b);
  65.             Console.WriteLine(--a);
  66.             Console.WriteLine(a--);
  67.  
  68.             Console.WriteLine(2 + (4 / 2 - 4) % 2);
  69.  
  70.             // Operators for comparison
  71.             Console.WriteLine(a > b);
  72.             Console.WriteLine(a < b);
  73.             Console.WriteLine(a >= b);
  74.             Console.WriteLine(a <= b);
  75.             Console.WriteLine(a == b);
  76.             Console.WriteLine(a != b);
  77.  
  78.             // Logical operators
  79.             bool x = true;
  80.             bool y = false;
  81.  
  82.             Console.WriteLine(x && y);
  83.             Console.WriteLine(x || y);
  84.             Console.WriteLine(x ^ y);
  85.             Console.WriteLine(!x);
  86.  
  87.             // Asignment opertors
  88.             a += 3; // a = a + 3;
  89.  
  90.             // Bitwise operators
  91.             Console.WriteLine(1 << 1);
  92.             Console.WriteLine(2 >> 1);
  93.             Console.WriteLine(1 | 2);
  94.             Console.WriteLine(1 & 2);
  95.             Console.WriteLine(1 ^ 3);// 2
  96.  
  97.             string name = "Stoyan";
  98.             string familyName = "Cheresharov";
  99.  
  100.             // COncatination operator +
  101.             Console.WriteLine(name + " " + familyName + a);
  102.  
  103.             int test1 = 11;
  104.             int test2 = 2;
  105.             Console.WriteLine(test1 / (float)test2);
  106.  
  107.             byte petya1 = 4; // unsigned integers 0-255
  108.             sbyte petya2 = -2;
  109.            
  110.             Console.WriteLine(petya1 + petya2);
  111.  
  112.             char myTest = (petya1 < petya2) ? 'C' : 'E';
  113.             Console.WriteLine(myTest);
  114.  
  115.             Console.WriteLine("Hello Emil, Evgeni!");
  116.             // System.Console.Beep(1000, 1000);
  117.  
  118.             // 3 Writing
  119.             // 3.1 Write
  120.             System.Console.Write("Please enter somethig: ");
  121.             // 3.2 WriteLine
  122.             System.Console.WriteLine("Emil");
  123.  
  124.             Console.WriteLine("Spas!!!");
  125.             // 1
  126.             Console.WriteLine("The favoritn number of Petya is " + petya1);
  127.  
  128.             // 2
  129.             Console.WriteLine("Petya has a little lamb! It has {0} legs. And the seconnd favorit number is {1}", petya1, petya2);
  130.  
  131.             // 3
  132.             Console.WriteLine($"Petya has a little lamb! It has {petya1} legs. And the seconnd favorit number is {petya2}");
  133.  
  134.             Console.OutputEncoding = Encoding.UTF8;
  135.  
  136.             Thread.CurrentThread.CurrentCulture =   CultureInfo.GetCultureInfo("bg-Bg");
  137.             // CultureInfo.GetCultureInfo("ro-Ro");
  138.             // CultureInfo.InvariantCulture;
  139.  
  140.             byte bestNumber = 5;
  141.  
  142.             Console.WriteLine("Best number = {0}", bestNumber);
  143.  
  144.             Console.WriteLine("{0,11:C2}|{1,10}",bestNumber, 7);
  145.  
  146.             Console.WriteLine("{0:F2}", 2345.4545364654);
  147.  
  148.             decimal spasBalance = 5.05m;
  149.  
  150.             Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-Gb");
  151.  
  152.             Console.WriteLine("{0:C2}", spasBalance);
  153.  
  154.             Console.WriteLine("{0:D20}", 123434566789);
  155.  
  156.             Console.WriteLine("{0,10:E8}", -1234.5);
  157.  
  158.             Console.WriteLine("{0,10:F2}", -123.456);
  159.             Console.WriteLine("{0,10:N2}", -1234.56);
  160.  
  161.             Console.WriteLine("{0,10:P2}", 0.45656);
  162.  
  163.             Console.WriteLine("{0,10:X5}", 254);
  164.             // Custom Formatting Symbols 0000, ###, %, E0 Е+0 Е-0
  165.             Console.WriteLine("{0:0.00}", 1); //Output: 1,00
  166.             Console.WriteLine("{0:#.##}", 0.234); //Output: ,23
  167.             Console.WriteLine("{0:#####}", 12345.67); //Output: 12346
  168.             Console.WriteLine("{0:(0#) ### ## ##}", 29342525); //Output: (02) 934 25 25
  169.             Console.WriteLine("{0:%##}", 0.234);//Output: %23
  170.  
  171.             // Standard Formatting Symbols for DateTime
  172.             DateTime d = new DateTime(2018, 12, 12, 11, 05, 22);
  173.  
  174.             Console.WriteLine("{0:D}", d); // d D
  175.             Console.WriteLine("{0:t}", d); // t T
  176.             Console.WriteLine("{0:Y}", d); // y Y
  177.  
  178.             // Custom Formatting Symbols for DateTime d, dd, M, MM, yy, yyyy, hh, HH, m, mm, s, ss
  179.             Console.WriteLine("{0:dd/MM/yyyy HH:mm:ss}", d);
  180.             Console.WriteLine("{0:d.MM.yy г.}", d);
  181.  
  182.             Console.BackgroundColor = ConsoleColor.DarkBlue;
  183.             Console.Clear();
  184.  
  185.             Console.BackgroundColor = ConsoleColor.Green;
  186.             Console.ForegroundColor = ConsoleColor.Red;
  187.  
  188.             Console.SetCursorPosition(50, 15); // left top
  189.             Console.WriteLine("Elena");
  190.  
  191.             Console.BackgroundColor = ConsoleColor.Black;
  192.             Console.Clear();
  193.             Console.ForegroundColor = ConsoleColor.White;
  194.  
  195.  
  196.             // Read
  197.             //Console.Write("Please eneter your name: ");
  198.             //int name2 = Console.Read();
  199.  
  200.             //Console.WriteLine("Welcome {0}!", name2);
  201.  
  202.             //Console.Write("Please, eneter your name: ");
  203.             //string name2 = Console.ReadLine();
  204.  
  205.             //Console.WriteLine("Welcome {0}!", name2);
  206.             ////1.
  207.             //Console.Write("Please, enter your age: ");
  208.             //int age = int.Parse(Console.ReadLine()); // 45
  209.  
  210.             //Console.WriteLine("Your age in 10 years time will be {0}", age + 10);
  211.  
  212.             //Console.Write("Please, enter your balance: ");
  213.             //decimal balance = decimal.Parse(Console.ReadLine());
  214.  
  215.             //// 2
  216.             //Console.Write("Please enter int number or something stupid: ");
  217.             //int myVar;
  218.             //if (!int.TryParse(Console.ReadLine(), out myVar))
  219.             //{
  220.             //    Console.WriteLine("Please be polite!");
  221.             //}
  222.  
  223.             // 3
  224.             // int converted = Convert.ToInt32(Console.ReadLine());
  225.  
  226.             //if (myVar > 5)
  227.             //{
  228.             //    Console.WriteLine("myVar is greater than 5!");
  229.             //}
  230.             //else if (myVar < 0 && myVar > -10)
  231.             //{
  232.             //    Console.WriteLine("myVar < 0 && myVar > -10");
  233.             //}
  234.             //else
  235.             //{
  236.             //    Console.WriteLine("myVar is lower than 5!");
  237.             //}
  238.  
  239.  
  240.             //Console.Write("Please, enter your name: ");
  241.             //int firstLetter = Console.Read();
  242.  
  243.             //switch ((char)firstLetter)
  244.             //{
  245.             //    case 'S':
  246.             //        Console.WriteLine("Mybe your name is Stoyan");
  247.             //        break;
  248.             //    case 'E':
  249.             //        Console.WriteLine("Mybe your name is Elena");
  250.             //        break;
  251.             //    default:
  252.             //        Console.WriteLine("I give up!");
  253.             //        break;
  254.             //}
  255.  
  256.  
  257.             // Console.WriteLine(1);
  258.             // Console.WriteLine(2);
  259.  
  260.             int i = 0;
  261.             while (i < 100)
  262.             {
  263.                 Console.WriteLine(i);
  264.                 i++;
  265.             }
  266.  
  267.             int number = 0;
  268.             do
  269.             {
  270.                 Console.Write("Please, enter a number between 3 and 30: ");
  271.                 number = int.Parse(Console.ReadLine());
  272.             } while (number < 3 || number > 30);
  273.  
  274.             for (int j = 0; j < 100; j++)
  275.             {
  276.                 if (j % 2 != 0)
  277.                 {
  278.                     // Console.WriteLine(j);
  279.                     continue;
  280.                 }
  281.  
  282.                 Console.WriteLine(j);
  283.                 if (j == 5)
  284.                 {
  285.                     break;
  286.                 }      
  287.             }
  288.  
  289.             int[] numbers = { 2, 3, 5, 7, 11, 13, 17, 19 };
  290.  
  291.             foreach (int k in numbers)
  292.             {
  293.                 Console.Write(" " + k);
  294.             }
  295.             Console.WriteLine();
  296.  
  297.             string[] students = { "Petyta", "Nadya", "Svetla", "Slavka", "Angela", "Dimitar", "Irina", "Marti", "Spas", "Evgeni", "Elena", "Atanas", "Polina", "Polya", "Georgi", "Emil", "Dimitar", "Simona" };
  298.  
  299.             foreach (var item in students)
  300.             {
  301.                 Console.WriteLine(item);
  302.             }
  303.  
  304.             int[,] matrix =
  305.             {
  306.                 {1, 2, 3, 4}, // row 0 values
  307.                 {5, 6, 7, 8}, // row 1 values
  308.             };
  309.  
  310.             Console.WriteLine(matrix.GetLength(0));
  311.             Console.WriteLine(matrix.GetLength(1));
  312.  
  313.  
  314.             int[][] myJaggedArray = {
  315.                 new int[] {5, 7, 2},
  316.                 new int[] {10, 20, 40},
  317.                 new int[] {3, 25},
  318.                 new int[] { 1,2,3,4,5,6,78,9}
  319.             };
  320.  
  321.             int[][,] jaggedOfMulti = new int[2][,];
  322.  
  323.             // another way to declare jagged array
  324.             int[][] jaggedArray;
  325.             jaggedArray = new int[2][];
  326.             jaggedArray[0] = new int[5];
  327.             jaggedArray[1] = new int[3];
  328.  
  329.  
  330.             //  IV
  331.             //0 - 9-> base(radix) 10
  332.             //0 - 1-> 2
  333.  
  334.             //37
  335.  
  336.             //3 * 10 ^ 1 + 7 * 10 ^ 0 = 3 * 10 + 7 * 1 = 30 + 7 = 37
  337.  
  338.             //1011
  339.  
  340.             //1 * 2 ^ 3 + 0 * 2 ^ 2 + 1 * 2 ^ 1 + 1 * 2 ^ 0 = 1 * 8 + 0 + 1 * 2 + 1 * 1 = 11
  341.  
  342.             //37 : 2 = 18  1
  343.             //18 : 2 = 9   0
  344.             //9 : 2 = 4    1
  345.             //4 : 2 = 2    0
  346.             //2 : 2 = 1    0
  347.             //             1
  348.  
  349.  
  350.             //100101
  351.  
  352.             //1 * 2 ^ 5 + 0 * 2 ^ 4 + 0 * 2 ^ 3 + 1 * 2 ^ 2 + 0 * 2 ^ 1 + 1 * 2 ^ 0 = 1 * 32 + 4 + 1 = 37
  353.  
  354.             //0 - 9...ABCDEF
  355.  
  356.             //1A
  357.  
  358.  
  359.             //1 * 16 ^ 1 + 10 * 16 ^ 0 = 16 + 10 = 26
  360.  
  361.             //0001 1010
  362.  
  363.             //0 - 7
  364.  
  365.             //17(8)
  366.  
  367.             //1 * 8 ^ 1 + 7 * 8 ^ 0 = 8 + 7 = 15
  368.  
  369.  
  370.             //0010 1110 0101 0101 0101 0101
  371.             //2    E    5    5    5    5
  372.  
  373.             //a - 10
  374.             //b - 11
  375.             //c - 12
  376.             //d - 13
  377.             //e - 14
  378.  
  379.  
  380.             //001 011 100 101 010 101 010 101
  381.             //1   3   4   5   2   5   2   5(8)
  382.  
  383.             // 9. Methods
  384.             // 0! = 1
  385.             // n! = (n-1)! * n
  386.             // 3! = 1 * 2 * 3
  387.  
  388.             // 4!
  389.             int factorial = 1;
  390.             for (int f = 1; f < 5; f++)
  391.             {
  392.                 factorial *= f;
  393.             }
  394.             Console.WriteLine($"Factorial 4! = {factorial}");
  395.  
  396.             // 5!
  397.             factorial = 1;
  398.             for (int f = 1; f < 6; f++)
  399.             {
  400.                 factorial *= f;
  401.             }
  402.             Console.WriteLine($"Factorial 5! = {factorial}");
  403.  
  404.             int fn = CalculateFactorial(10);
  405.  
  406.             Console.WriteLine(fn);
  407.  
  408.             int[] myInputArray = new int[6];
  409.  
  410.             InputArray(myInputArray);
  411.  
  412.             //for (int m = 0; m < myInputArray.Length; m++)
  413.             //{
  414.             //    Console.Write("Please, enter myInputArray[{0}] =  ", m);
  415.             //    myInputArray[m] = int.Parse(Console.ReadLine());
  416.             //}
  417.  
  418.  
  419.             //myInputArray = new int[8];
  420.  
  421.             //for (int m = 0; m < myInputArray.Length; m++)
  422.             //{
  423.             //    Console.Write("Please, enter myInputArray[{0}] =  ", m);
  424.             //    myInputArray[m] = int.Parse(Console.ReadLine());
  425.             //}
  426.  
  427.             Console.WriteLine(Factorial(100));
  428.         }
  429.  
  430.         static int CalculateFactorial(int f)
  431.         {
  432.             int factorial = 1;
  433.             for (int i = 1; i <= f; i++)
  434.             {
  435.                 factorial *= i;
  436.             }
  437.             return factorial;
  438.         }
  439.  
  440.         // 0! = 1
  441.         // n! = (n-1)! * n
  442.         static int Factorial(int n)
  443.         {
  444.             if (n == 0) return 1;
  445.             return Factorial(n - 1) * n;
  446.         }
  447.  
  448.  
  449.         static void InputArray(int[] myInputArray)
  450.         {
  451.             for (int m = 0; m < myInputArray.Length; m++)
  452.             {
  453.                 Console.Write("Please, enter myInputArray[{0}] =  ", m);
  454.                 myInputArray[m] = int.Parse(Console.ReadLine());
  455.             }
  456.         }
  457.            
  458.     }
  459. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement