Advertisement
wingman007

IntroProgrammingC#ExcercisesPartTime

Sep 17th, 2023 (edited)
646
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.70 KB | None | 0 0
  1. // Exercises
  2. // I. first day
  3. // 1. Hello World
  4. using System.Security.Cryptography.X509Certificates;
  5.  
  6. Greet("Ilkay");
  7.  
  8. // 2. Variables, Primitive data Types Literals
  9. Console.WriteLine("Test");
  10. int a = 7;
  11. int b = 3;
  12. int speedOfLight = 0xe3; //300000;
  13. Console.WriteLine((float)a/b);
  14.  
  15. // 3. Operators
  16. Console.WriteLine(a*b > b*b*a || b + a < a*b);
  17.  
  18. // 4. Formatted output/ Reading from console
  19. Console.WriteLine("{0,10:C2}", b);
  20. Console.Write("Please, enter your age: ");
  21. string age = Console.ReadLine();
  22. int ageDigit = int.Parse(age);
  23. Console.WriteLine("In 10 years you will be {0}", ageDigit + 10);
  24.  
  25. // Console.Write("Please, tell me how much money do you have: ");
  26.  
  27. // 5. Conditional Logic
  28. //Console.Write("Please enter your age: ");
  29. //string aaa = Console.ReadLine();
  30. //int age1 = int.Parse(Console.ReadLine());
  31.  
  32. ////if (age1 >= 0 && age1 <= 3)
  33. ////{
  34. ////    Console.WriteLine("You are a baby");
  35. ////}
  36.  
  37. //if (age1 >= 0 && age1 <=3)
  38. //{
  39. //    Console.WriteLine("You are a baby");
  40. //}
  41. //else if (age1 >= 3 && age1 <= 13)
  42. //{
  43. //    Console.WriteLine("You are a kid");
  44. //}
  45. //else if (age1 > 13 && age1 <= 18)
  46. //{
  47. //    Console.WriteLine("You are teenager!");
  48. //}
  49. //else
  50. //{
  51. //    Console.WriteLine("You are an aduld");
  52. //}
  53.  
  54. //Console.Write("Enter the first letter of your name: ");
  55. //char firstLetter = char.Parse(Console.ReadLine());
  56.  
  57. //switch (firstLetter)
  58. //{
  59. //    case 'S':
  60. //        Console.WriteLine("You are a lucki man");
  61. //        break;
  62. //    case 'D':
  63. //        Console.WriteLine("You are a great man");
  64. //        break;
  65. //    default:
  66. //        Console.WriteLine("I don't know what to say");
  67. //        break;
  68. //}
  69.  
  70. // 6. Loops
  71. for (int i = 0; i < 10; i++)
  72. {
  73.     Console.WriteLine(i);
  74. }
  75.  
  76. // II Second day of Excercises
  77. //7. Arrays
  78. // Declare an array of 10 Ints assign values and print them on the console
  79. string[] namesOfStudents = new string[] {
  80.     "Yaren",
  81.     "Darina",
  82.     "Aishe",
  83.     "Suzana",
  84.     "Momchil",
  85.     "Ivan...14",
  86.     "Stanislava",
  87.     "Ilker",
  88.     "Ilkay",
  89.     "Dean",
  90.     "Nikola"
  91. };
  92.  
  93. Console.WriteLine("Hello Deny!");
  94.  
  95. int[] numbers = new int[10];
  96. numbers[0] = 10;
  97. numbers[1] = 12;
  98.  
  99. foreach (var n in numbers)
  100. {
  101.     Console.WriteLine(n);
  102. }
  103. Greet("Stanislava");
  104. int number;
  105. do
  106. {
  107.     Console.Write("Please enter a number between 1 - 5: ");
  108.     number = int.Parse(Console.ReadLine());
  109.  
  110. } while (number < 1 || number > 5);
  111.  
  112. // 8. Number systems
  113. Console.WriteLine("2093 to BIN {0}.", Convert.ToString(2093, 2));
  114. Console.WriteLine("2093 to OCT {0}.", Convert.ToString(2093, 8));
  115. Console.WriteLine("2093 to HEX {0}.", Convert.ToString(2093, 16));
  116.  
  117. // 9. Methods
  118. // Declare and define a method which grrets. First without formal parameters and returned value. After that with a formal parameter
  119. Console.WriteLine(AddNumbers(34, 54));
  120. static void Greet(string name = "Deny")
  121. {
  122.     Console.WriteLine($"Hello {name}!");
  123. }
  124.  
  125. // Declare and define a function with a returned value which adds two digits
  126. static int AddNumbers(int a, int b)
  127. {
  128.     return a + b;
  129. }
  130.  
  131. static bool IsOdd(int num)
  132. {
  133.     return (num % 2 == 1)? true : false;
  134. }
  135.  
  136. // 10. Recursion
  137. // With the help of ChatGPT create a function factorial
  138. // factorial
  139. // 0! = 1
  140. // n! = (n-1)! * n
  141. static int Factorial(int n)
  142. {
  143.     if (n == 0)
  144.     {
  145.         return 1;
  146.     }
  147.     else
  148.     {
  149.         return n * Factorial(n - 1);
  150.     }
  151. }
  152.  
  153.  
  154. Console.WriteLine(Factorial(3));
  155.  
  156. static double FactorialIter(int n)
  157. {
  158.     double factorial = 1;
  159.     for (int i = 2; i <= n; i++)
  160.     {
  161.         factorial *= i;
  162.     }
  163.     return factorial;
  164. }
  165.  
  166. // III. Excercise
  167. // 1. Define a function that finds the min element in array of ints
  168.  
  169.  
  170. //int[] nums = {1,2,3,4,5,6,7,8,9};
  171.  
  172. //void FindLowest()
  173. //{
  174. //    int lowest = int.MaxValue;
  175. //    for (int i = 0; i < nums.Length; i++)
  176. //    {
  177. //        int current = nums[i];
  178. //        if (current < lowest)
  179. //        {
  180. //            current = lowest;
  181. //        }
  182. //    }
  183. //    Console.WriteLine( lowest);
  184. //};
  185. //FindLowest();
  186.  
  187.  
  188. // Min
  189. static int FindMin(int[] nArray)
  190. {
  191.     int min = nArray[0];
  192.     foreach (int num in nArray)
  193.     {
  194.         if (min > num) { min = num; }
  195.     }
  196.     return min;
  197. }
  198.  
  199. // Max
  200. static int FindMax(int[] nArray)
  201. {
  202.     int max = nArray[0];
  203.     foreach (int num in nArray)
  204.     {
  205.         if (max < num) { max = num; }
  206.     }
  207.     return max;
  208. }
  209.  
  210. // Sort
  211. static void BubbleSortDescending(int[] arr)
  212. {
  213.     int temp;
  214.     for (int i = 0; i < arr.Length - 1; i++)
  215.     {
  216.         for (int j = 0; j < arr.Length - i - 1; j++)
  217.         {
  218.             if (arr[j] < arr[j + 1]) // For ascending order, use '>'
  219.             {
  220.                 // Swap the elements
  221.                 temp = arr[j];
  222.                 arr[j] = arr[j + 1];
  223.                 arr[j + 1] = temp;
  224.             }
  225.         }
  226.     }
  227. }
  228.  
  229. // Filter
  230. static int[] FilterOddNumbers(int[] arr)
  231. {
  232.     int count = 0;
  233.     for (int i = 0; i < arr.Length; i++)
  234.     {
  235.         if (arr[i] % 2 != 0)
  236.         {
  237.             count++;
  238.         }
  239.     }
  240.  
  241.     int[] result = new int[count];
  242.     int index = 0;
  243.     for (int i = 0; i < arr.Length; i++)
  244.     {
  245.         if (arr[i] % 2 != 0)
  246.         {
  247.             result[index] = arr[i];
  248.             index++;
  249.         }
  250.     }
  251.  
  252.     return result;
  253. }
  254.  
  255.  
  256. int[] nums = { 23, 29, 354, 432, 765, 89, 90, 87, 9, 10 };
  257. Console.WriteLine("Find Min = {0}", FindMin(nums));
  258. Console.WriteLine("Find Min = {0}", FindMax(nums));
  259.  
  260.  
  261. // int[] oddNums = nums.Where(n => n % 2 != 0).ToArray();
  262. int[] oddNums = FilterOddNumbers(nums);
  263. foreach (var num in oddNums)
  264. {
  265.     Console.WriteLine(num);
  266. }
  267.  
  268. BubbleSortDescending(nums);
  269.  
  270. // Print the sorted array
  271. foreach (var num in nums)
  272. {
  273.     Console.WriteLine(num);
  274. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement