Advertisement
EmoRz

DataTypeVariables_Exercises

May 16th, 2018
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Numerics;
  4.  
  5. namespace DataTypeVariables
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.         }
  12.  
  13.         private static void Compare_FloatingNumbers()
  14.         {
  15.             var diff = 0.0;
  16.             var a = double.Parse(Console.ReadLine());
  17.             var b = double.Parse(Console.ReadLine());
  18.             //
  19.             const double eps = 0.000001;
  20.             diff = a - b;
  21.             diff = Math.Abs(diff);
  22.             if (diff < eps)
  23.             {
  24.                 Console.WriteLine("True");
  25.             }
  26.             else
  27.             {
  28.                 Console.WriteLine("False");
  29.             }
  30.             //Console.WriteLine(diff);
  31.         }
  32.  
  33.         private static void DifferentInteger_Size()
  34.         {
  35.             //Console.WriteLine(long.MaxValue); ///9223372036854775807
  36.             //Console.WriteLine(value: sbyte.MinValue); //
  37.             var integer = BigInteger.Parse(Console.ReadLine());
  38.             //
  39.             List<string> printData = new List<string>();
  40.  
  41.             var sbytee = integer >= sbyte.MinValue && integer <= sbyte.MaxValue;
  42.             var bytee = integer >= byte.MinValue && integer <= byte.MaxValue;
  43.             var shortt = integer >= short.MinValue && integer <= short.MaxValue;
  44.             var ushortt = integer >= ushort.MinValue && integer <= ushort.MaxValue;
  45.             var intt = integer >= int.MinValue && integer <= int.MaxValue;
  46.             var uintt = integer >= uint.MinValue && integer <= uint.MaxValue;
  47.             var longg = integer >= long.MinValue && integer <= long.MaxValue;
  48.             var ulongg = integer >= ulong.MinValue && integer <= ulong.MaxValue;
  49.             //
  50.             if (sbytee)
  51.             {
  52.                 var temp = "* sbyte";
  53.                 printData.Add(temp);
  54.             }
  55.             if (bytee)
  56.             {
  57.                 var temp = "* byte";
  58.                 printData.Add(temp);
  59.             }
  60.             if (shortt)
  61.             {
  62.                 var temp = "* short";
  63.                 printData.Add(temp);
  64.             }
  65.             if (ushortt)
  66.             {
  67.                 var temp = "* ushort";
  68.                 printData.Add(temp);
  69.             }
  70.             if (intt)
  71.             {
  72.                 var temp = "* int";
  73.                 printData.Add(temp);
  74.             }
  75.             if (uintt)
  76.             {
  77.                 var temp = "* uint";
  78.                 printData.Add(temp);
  79.             }
  80.             if (longg)
  81.             {
  82.                 var temp = "* long";
  83.                 printData.Add(temp);
  84.             }
  85.  
  86.             if (sbytee || bytee || shortt || ushortt || intt || uintt || longg)
  87.             {
  88.                 Console.WriteLine($"{integer} can fit in:");
  89.                 foreach (var data in printData)
  90.                 {
  91.                     Console.WriteLine(data);
  92.                 }
  93.             }
  94.             else
  95.             {
  96.                 Console.WriteLine($"{integer} can't fit in any type");
  97.             }
  98.         }
  99.  
  100.         private static void ThePhotographer()
  101.         {
  102.             //Thea => the Photographer :)
  103.             var numPictures = int.Parse(Console.ReadLine());
  104.             var filterTime = int.Parse(Console.ReadLine());
  105.             var percentOfGoodPic = Math.Ceiling(double.Parse(Console.ReadLine()));
  106.             var uploadTime = int.Parse(Console.ReadLine());
  107.             //
  108.             var sortetPic = (numPictures * (percentOfGoodPic)) / 100.0;
  109.             //
  110.             long timeFilter = numPictures * filterTime;
  111.             long timeUpload = (long)(Math.Ceiling(sortetPic) * uploadTime);
  112.             long allTime = timeUpload + timeFilter;
  113.             //
  114.             TimeSpan t = TimeSpan.FromSeconds(allTime);
  115.             string printTime = string.Format($"{t.Days}:{t.Hours:d2}:{t.Minutes:d2}:{t.Seconds:d2}");
  116.             Console.WriteLine(printTime);
  117.         }
  118.  
  119.         private static void Print_Character()
  120.         {
  121.             int start = int.Parse(Console.ReadLine());
  122.             int end = int.Parse(Console.ReadLine());
  123.             //
  124.             string temp = "";
  125.             //
  126.             List<int> num = new List<int>();
  127.             for (int i = start; i <= end; i++)
  128.             {
  129.                 num.Add(i);
  130.             }
  131.             foreach (char item in num)
  132.             {
  133.                 temp += item + " ";
  134.             }
  135.             Console.WriteLine(temp);
  136.         }
  137.  
  138.         private static void Refacktor_PrimeNumber()
  139.         {
  140.             int num = int.Parse(Console.ReadLine());
  141.             for (int i = 2; i <= num; i++)
  142.             {
  143.                 bool checkForPrime = true;
  144.                 for (int j = 2; j <= Math.Sqrt(i); j++)
  145.                 {
  146.                     if (i % j == 0)
  147.                     {
  148.                         checkForPrime = false;
  149.                         break;
  150.                     }
  151.                 }
  152.                 Console.WriteLine($"{i} -> {checkForPrime}");
  153.             }
  154.         }
  155.  
  156.         private static void Hexadecima_Binary()
  157.         {
  158.             var num = int.Parse(Console.ReadLine());
  159.             var hexadecimal = (Convert.ToString(num, 16).ToUpper());
  160.             string binary = Convert.ToString(num, 2);
  161.             Console.WriteLine(hexadecimal);
  162.             Console.WriteLine((binary));
  163.         }
  164.  
  165.         private static void Vowel_Digit()
  166.         {
  167.             var input = Console.ReadLine();
  168.             var print = "";
  169.             if (input == "a" || input == "A" || input == "O" || input == "o" || input == "I" || input == "i" || input == "e" || input == "E"
  170.                 || input == "U" || input == "u")
  171.             {
  172.                 print = "vowel";
  173.             }
  174.             else if (input == "0" || input == "1" || input == "2" || input == "3" || input == "4" || input == "5" || input == "6" || input == "7" || input == "8" ||
  175.                 input == "9")
  176.             {
  177.                 print = "digit";
  178.             }
  179.             else
  180.             {
  181.                 print = "other";
  182.             }
  183.             Console.WriteLine(print);
  184.         }
  185.  
  186.         private static void Rectangle_Properties()
  187.         {
  188.             double a = double.Parse(Console.ReadLine());
  189.             double b = double.Parse(Console.ReadLine());
  190.             //
  191.             var perimeter = (a * 2) + (b * 2);
  192.             var area = a * b;
  193.             var diagonal = a * a + b * b;
  194.             Console.WriteLine(perimeter);
  195.             Console.WriteLine(area);
  196.             Console.WriteLine(Math.Sqrt(diagonal));
  197.         }
  198.  
  199.         private static void Convert_Metrics()
  200.         {
  201.             int distanceMeters = int.Parse(Console.ReadLine());
  202.             var hours = decimal.Parse(Console.ReadLine());
  203.             var minutes = decimal.Parse(Console.ReadLine());
  204.             var seconds = decimal.Parse(Console.ReadLine());
  205.             //
  206.             var metersPerSeconds = ((hours * 60) + minutes) * 60;
  207.             float meterPerSec = (float)distanceMeters / (float)metersPerSeconds;
  208.             //
  209.             var metersToKm = distanceMeters / 1000;
  210.             var time = (((seconds / 60) + minutes) / 60) + hours;
  211.             float kmPerHours = (float)metersToKm / (float)time;
  212.             //
  213.             //Assume 1 mile = 1609 meters.
  214.             var metersToMile = distanceMeters / 1609.0;
  215.             var timeMile = (((seconds / 60) + minutes) / 60) + hours;
  216.             float milePerHour = (float)metersToMile / (float)timeMile;
  217.  
  218.             Console.WriteLine($"{Math.Round(meterPerSec, 7)}");
  219.             Console.WriteLine($"{Math.Round(kmPerHours, 7)}");
  220.             Console.WriteLine($"{Math.Round(milePerHour, 7)}");
  221.         }
  222.  
  223.         private static void CenturiesToNanoseconds()
  224.         {
  225.             byte centuries = byte.Parse(Console.ReadLine());
  226.             int years = centuries * 100;
  227.             int days = (int)((double)years * 365.2422);
  228.             long hours = days * 24;
  229.             long minutes = hours * 60;
  230.             long secundes = minutes * 60;
  231.             long millieseconds = secundes * 1000;
  232.             decimal microseconds = millieseconds * 1000;
  233.             decimal nanoseconds = microseconds * 1000;
  234.             Console.WriteLine($"{centuries} centuries = {years} years = {days} days = {hours} hours = {minutes}" +
  235.                 $" minutes = {secundes} seconds = {millieseconds}" +
  236.                 $" milliseconds = {microseconds} microseconds = {nanoseconds} nanoseconds");
  237.         }
  238.  
  239.         private static void ReverseString()
  240.         {
  241.             string letter1 = Console.ReadLine();
  242.             string letter2 = Console.ReadLine();
  243.             string letter3 = Console.ReadLine();
  244.  
  245.             string str = (letter1 + letter2 + letter3);
  246.             string revers = "";
  247.             for (int i = str.Length - 1; i >= 0; i--)
  248.             {
  249.                 revers += str[i];
  250.             }
  251.             Console.WriteLine(revers);
  252.         }
  253.  
  254.         private static void EmployeeData()
  255.         {
  256.             string firstName = Console.ReadLine();
  257.             string secondName = Console.ReadLine();
  258.             byte age = byte.Parse(Console.ReadLine());
  259.             char sex = char.Parse(Console.ReadLine());
  260.             long personalIdNumber = long.Parse(Console.ReadLine());
  261.             int UEN = int.Parse(Console.ReadLine());
  262.             //
  263.             Console.WriteLine($"First name: {firstName}");
  264.             Console.WriteLine($"Last name: {secondName}");
  265.             Console.WriteLine($"Age: {age}");
  266.             Console.WriteLine($"Gender: {sex}");
  267.             Console.WriteLine($"Personal ID: {personalIdNumber}");
  268.             Console.WriteLine($"Unique Employee number: {UEN}");
  269.         }
  270.  
  271.         private static void Exchange_Variables_Value()
  272.         {
  273.             var a = 5;
  274.             var b = 10;
  275.             var c = 0;
  276.  
  277.             Console.WriteLine("Before:");
  278.             Console.WriteLine($"a = {a}");
  279.             Console.WriteLine($"b = {b}");
  280.             Console.WriteLine("After:");
  281.             c = a;
  282.             a = b;
  283.             b = c;
  284.             Console.WriteLine($"a = {a}");
  285.             Console.WriteLine($"b = {b}");
  286.         }
  287.  
  288.         private static void String_Object()
  289.         {
  290.             string hello = "Hello";
  291.             string world = "World";
  292.             object obj = hello + " " + world;
  293.             string str = (string)obj;
  294.             Console.WriteLine(str);
  295.         }
  296.  
  297.         private static void Bool_Variable()
  298.         {
  299.             string input = Console.ReadLine();
  300.             var isTrue = Convert.ToBoolean(input);
  301.             if (isTrue)
  302.             {
  303.                 Console.WriteLine("Yes");
  304.             }
  305.             else
  306.             {
  307.                 Console.WriteLine("No");
  308.             }
  309.         }
  310.  
  311.         private static void Hexadecimal()
  312.         {
  313.             string data = Console.ReadLine();
  314.             var n = Convert.ToInt32(data, 16);
  315.             Console.WriteLine(n.ToString());
  316.         }
  317.  
  318.         private static void String_Char()
  319.         {
  320.             string f = "Software University";
  321.             char b = 'B';
  322.             char y = 'y';
  323.             char e = 'e';
  324.             string s = "I love programming";
  325.             Console.WriteLine(f);
  326.             Console.WriteLine(b);
  327.             Console.WriteLine(y);
  328.             Console.WriteLine(e);
  329.             Console.WriteLine(s);
  330.         }
  331.  
  332.         private static void Floating_Numbers()
  333.         {
  334.             decimal n1 = 3.141592653589793238m;
  335.             double n2 = 1.60217657;
  336.             decimal n3 = 7.8184261974584555216535342341M;
  337.             Console.WriteLine(n1);
  338.             Console.WriteLine(n2);
  339.             Console.WriteLine(n3);
  340.         }
  341.  
  342.         private static void Integers()
  343.         {
  344.             sbyte num1 = -100;
  345.             byte num2 = 128;
  346.             short num3 = -3540;
  347.             ushort num4 = 64876;
  348.             uint num5 = 2147483648;
  349.             int num6 = -1141583228;
  350.             long num7 = -1223372036854775808;
  351.             //
  352.             Console.WriteLine(num1);
  353.             Console.WriteLine(num2);
  354.             Console.WriteLine(num3);
  355.             Console.WriteLine(num4);
  356.             Console.WriteLine(num5);
  357.             Console.WriteLine(num6);
  358.             Console.WriteLine(num7);
  359.         }
  360.     }
  361. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement