mitko2204

Course Work - C#

Nov 9th, 2022 (edited)
714
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.68 KB | None | 0 0
  1. using System;
  2.                    
  3. public class Program
  4. {
  5.     public static void Main()
  6.     {
  7.         //зад. 1
  8.         //2025
  9.         static void Main(string[] args)
  10.         {
  11.             int x = 2042;
  12.             ConvertBinary(x);
  13.             ConvertOctal(x);
  14.             Console.WriteLine();
  15.             Console.WriteLine("Hexadecimal: " + ConvertHexadecimal(x));
  16.         }
  17.  
  18.         private static void ConvertBinary(int decimalNumber)
  19.         {
  20.             int remainder;
  21.             string result = string.Empty;
  22.             while (decimalNumber > 0)
  23.             {
  24.                 remainder = decimalNumber % 2;
  25.                 decimalNumber /= 2;
  26.                 result = remainder.ToString() + result;
  27.             }
  28.             Console.WriteLine("Binary:  {0}", result);
  29.         }
  30.         private static void ConvertOctal(int decValue)
  31.         {
  32.             int counter, i = 1, j;
  33.             int[] octalValue = new int[80];
  34.             counter = decValue;
  35.             while (counter != 0)
  36.             {
  37.                 octalValue[i++] = counter % 8;
  38.                 counter /= 8;
  39.             }
  40.             Console.Write("Octal Number: ");
  41.             for (j = i - 1; j > 0; j--)
  42.                 Console.Write(octalValue[j]);
  43.         }
  44.         private static string ConvertHexadecimal(int n)
  45.         {
  46.             if (n < 1) return "0";
  47.             int hex = n;
  48.             string hexStr = string.Empty;
  49.             while (n > 0)
  50.             {
  51.                 hex = n % 16;
  52.                 if (hex < 10)
  53.                     hexStr = hexStr.Insert(0, Convert.ToChar(hex + 48).ToString());
  54.                 else
  55.                     hexStr = hexStr.Insert(0, Convert.ToChar(hex + 55).ToString());
  56.                 n /= 16;
  57.             }
  58.             return hexStr;
  59.         }
  60.  
  61.         //зад. 2
  62.         //Тест 1 - 2 II)
  63.             Console.Write("Enter B: ");
  64.             int B = int.Parse(Console.ReadLine());
  65.             Console.Write("Enter y:");
  66.             int y = int.Parse(Console.ReadLine());
  67.             Console.WriteLine((B + Math.Pow(Math.Sin(Math.Pow(Math.PI, 4)), 2)) / (Math.Pow(Math.Cos(6), ((double)1 / 5)) + Math.Abs((double)1 / Math.Tan(y))));
  68.  
  69.         //зад. 3
  70.         //63
  71.             Console.Write("Enter k: ");
  72.             int k = int.Parse(Console.ReadLine());
  73.             double y = -1;
  74.             if (k == 1)
  75.             {
  76.             y = 6.7;
  77.             }
  78.             else if (k == 2)
  79.             {
  80.             y = 6.7 + 9 * k;
  81.             }
  82.             else if (k == 3)
  83.             {
  84.             y = 6.7 + 9 * k + 7 * Math.Pow(k, 2);
  85.             }
  86.             Console.WriteLine($"y = {y}");
  87.  
  88.  
  89.         //зад. 4
  90.         //213ж
  91.             Console.WriteLine(TangensCalc(1));
  92.             double TangensCalc(int v)
  93.             {
  94.             if (v == 100)
  95.             {
  96.                 return Math.Tan(v);
  97.             }
  98.             return Math.Tan(v + TangensCalc(v + 1));
  99.             }
  100.  
  101.         //зад. 5
  102.         //379
  103.             Random random = new Random();
  104.             int[] arr = new int[132];
  105.             for (int i = 0; i < arr.Length; i++)
  106.             {
  107.                 arr[i] = (random.Next(0, 150));
  108.             }
  109.             for (int i = 0; i < arr.Length; i++)
  110.             {
  111.                 if (arr[i] % 2 != 0)
  112.                 {
  113.                     Console.WriteLine(arr[i]);
  114.                 }
  115.             }
  116.            
  117.             Console.Write("Enter 5 numbers: ");
  118.             int[] numbers = new int[5];
  119.             string[] input = Console.ReadLine().Split(' ');
  120.             for (int i = 0; i < numbers.Length; i++)
  121.                 numbers[i] = int.Parse(input[i]);
  122.             int proizvedenie = numbers[0] * numbers[1] * numbers[2] * numbers[3] * numbers[4];
  123.             Console.WriteLine(proizvedenie);
  124.             double koren = (Math.Pow(proizvedenie, ((double)1 / 5)));
  125.             Console.WriteLine(koren);
  126.            
  127.             Console.Write("Enter a= ");
  128.             int a = int.Parse(Console.ReadLine());
  129.             Console.Write("Enter b= ");
  130.             int b = int.Parse(Console.ReadLine());
  131.             Console.Write("Enter c= ");
  132.             int c = int.Parse(Console.ReadLine());
  133.             Console.WriteLine("Output is " + Sss(Math.Abs(c - b)) * Sss(a + c));
  134.             BigInteger Sss(int k)
  135.             {
  136.                 BigInteger product = 1;
  137.                 bool flag = true;
  138.                 Random r = new Random();
  139.                 for (int i = 0; i < k; i++)
  140.                 {
  141.                     int a = r.Next(0, 100);
  142.                     if ((a <= 99 && a >= 10) && a % 10 == 5)
  143.                     {
  144.                         flag = false;
  145.                         product *= a;
  146.                     }
  147.                 }
  148.                 if (flag)
  149.                     return 0;
  150.                 else
  151.                     return product;
  152.  
  153.             }
  154.  
  155.         //зад. 6
  156.         //398
  157.             Console.Write("Days in a month[1, 31]: ");
  158.             int n = int.Parse(Console.ReadLine());
  159.             while (n <= 0 && n > 31)
  160.             {
  161.             Console.WriteLine("Invalid days! Try again[1, 31]: ");
  162.             n = int.Parse(Console.ReadLine());
  163.             }
  164.             Console.WriteLine("Station A");
  165.             var stationA = Rains(n);
  166.             Console.WriteLine("Station B");
  167.             var stationB = Rains(n);
  168.             Console.WriteLine("Station C");
  169.             var stationC = Rains(n);
  170.             double arithmeticA = Arithmetic(stationA);
  171.             double arithmeticB = Arithmetic(stationB);
  172.             double arithmeticC = Arithmetic(stationC);
  173.             int overArithmeticA = 0, overArithmeticB = 0, overArithmeticC = 0;
  174.             for (int i = 0; i < n; i++)
  175.             {
  176.             if (stationA[i] > arithmeticA)
  177.             overArithmeticA++;
  178.             if (stationB[i] > arithmeticB)
  179.             overArithmeticB++;
  180.             if (stationC[i] > arithmeticC)
  181.             overArithmeticC++;
  182.             }
  183.             Console.WriteLine($"The Arithmetic of Station A is: {arithmeticA}! And has {overArithmeticA} days exceeding the arithmetic!");
  184.             Console.WriteLine($"The Arithmetic of Station B is: {arithmeticB}! And has {overArithmeticB} days exceeding the arithmetic!");
  185.             Console.WriteLine($"The Arithmetic of Station C is: {arithmeticC}! And has {overArithmeticC} days exceeding the arithmetic!");
  186.  
  187.             double Arithmetic(double[] days)
  188.             {
  189.             double sum = 0;
  190.             int n = days.Length;
  191.             for (int i = 0; i < n; i++)
  192.             sum += days[i];
  193.             return sum / n;
  194.             }
  195.  
  196.             double[] Rains(int days)
  197.             {
  198.             double[] rains = new double[days];
  199.             double n;
  200.             for (int i = 0; i < days; i++)
  201.             {
  202.             Console.Write($"Day {i + 1}: ");
  203.             n = double.Parse(Console.ReadLine());
  204.             if (n >= 0)
  205.             rains[i] = n;
  206.             else
  207.             i--;
  208.             }
  209.             Console.WriteLine();
  210.             return rains;
  211.             }
  212.  
  213.         //зад. 7
  214.         //372аб
  215.             static void Main(string[] args)
  216.             {
  217.             Console.Write("Въведете дължина на масива: ");
  218.             int n = int.Parse(Console.ReadLine());
  219.             while (n < 1 || n > 50)
  220.             {
  221.                 Console.WriteLine("Дължината трябва да е между 1 и 50!");
  222.                 n = int.Parse(Console.ReadLine());
  223.             }
  224.             string[] cities = new string[n];
  225.             InputElements(cities);
  226.             Console.WriteLine("Градове съдържащи 'град' или 'Град' в името");
  227.             StringContain(cities);
  228.             }
  229.  
  230.             private static void StringContain(string[] cities)
  231.             {
  232.             string s1 = "град";
  233.             string s2 = "Град";
  234.             foreach (var item in cities)
  235.             {
  236.                 if (item.Contains(s1) || item.Contains(s2))
  237.                 {
  238.                     Console.WriteLine(item);
  239.                 }
  240.             }
  241.             }
  242.  
  243.             private static void InputElements(string[] cities)
  244.             {
  245.             for (int i = 0; i < cities.Length; i++)
  246.             {
  247.                 Console.Write("Въведи град: ");
  248.                 string city = Console.ReadLine();
  249.                 cities[i] = FirstLetterToUpper(city);
  250.             }
  251.             }
  252.             public static string FirstLetterToUpper(string str)
  253.             {
  254.             if (str == null)
  255.                 return null;
  256.             else
  257.                 return char.ToUpper(str[0]) + str.Substring(1);
  258.             }
  259.     }
  260. }
Advertisement
Add Comment
Please, Sign In to add comment