wingman007

IntroProgrammingC#ExcercisesPartTime

Sep 17th, 2023 (edited)
1,636
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.91 KB | None | 0 0
  1. // Exercises
  2. // 2026 part-time students – 3 days
  3.  
  4. // 1. Start a computer and a suitable IDE, and create a program that prints "Hello, World!" to the console.
  5.  
  6. // 2. Define a variable with a suitable type and name that can hold the distance to the Moon. First, specify the value using the decimal numeral system, and then change the literal to represent the same value in hexadecimal.
  7.  
  8. // 3. Print to the console the remainder of dividing 15 by 2.
  9.  
  10. // 4. Ask the user to enter their age and tell them how old they will be in 10 years.
  11.  
  12. // 5. If the age from the previous task is greater than 40, print "You are old"; otherwise, print "You are young."
  13.  
  14. // 6. Print the numbers from 0 to 100 to the console.
  15.  
  16. // 7. Define an array of 10 integer values. Multiply each element by 2 and print the results.
  17.  
  18. // 8. Define a method named Fibonacci that returns the n-th element of the Fibonacci sequence. Print the first 100 elements of the Fibonacci sequence.
  19.  
  20. // 9. Define a method named FibonacciRecursion that does the same as the previous method, but uses recursion.
  21.  
  22. // 10. Define a function that finds the minimum element of an array. Define an array in the main program and print its minimum element.
  23.  
  24. // 11. Define a function that finds the maximum element of an array. Define an array in the main program and print its maximum element.
  25.  
  26. // 12. Define a function that returns only the odd elements of an array. Print the returned elements.
  27.  
  28. // 13. Define a function that sorts an array of integers using bubble sort.
  29.  
  30.  
  31.  
  32. // I. first day
  33. // 1. Hello World
  34. using System.Security.Cryptography.X509Certificates;
  35.  
  36. Greet("Ilkay");
  37.  
  38. // 2. Variables, Primitive data Types Literals
  39. // Task 2.1. distance2MoonKm = 384440; // 2.1.1. represent it in hexadecimal 0x5dd90// print it on the console. int.ToString("E");
  40. Console.WriteLine("Test");
  41. int a = 7;
  42. int b = 3;
  43. int speedOfLight = 0xe3; //300000;
  44. Console.WriteLine((float)a/b);
  45.  
  46. // 3. Operators
  47. // 3.1. Print the remaining fo the division of 15 an 2 15%2
  48. // Task 3.1.
  49. Console.WriteLine(a*b > b*b*a || b + a < a*b);
  50.  
  51. // 4. Formatted output/ Reading from console
  52. // 4.1. Read the age of a person add 10 and print what would be the age in 10 yeras.
  53. Console.WriteLine("{0,10:C2}", b);
  54. Console.Write("Please, enter your age: ");
  55. string age = Console.ReadLine();
  56. int ageDigit = int.Parse(age);
  57. Console.WriteLine("In 10 years you will be {0}", ageDigit + 10);
  58.  
  59. // Console.Write("Please, tell me how much money do you have: ");
  60.  
  61. // 5. Conditional Logic
  62. // 5.1. If the age is greater than 40 print you are old else print you are young
  63. //Console.Write("Please enter your age: ");
  64. //string aaa = Console.ReadLine();
  65. //int age1 = int.Parse(Console.ReadLine());
  66.  
  67. ////if (age1 >= 0 && age1 <= 3)
  68. ////{
  69. ////    Console.WriteLine("You are a baby");
  70. ////}
  71.  
  72. //if (age1 >= 0 && age1 <=3)
  73. //{
  74. //    Console.WriteLine("You are a baby");
  75. //}
  76. //else if (age1 >= 3 && age1 <= 13)
  77. //{
  78. //    Console.WriteLine("You are a kid");
  79. //}
  80. //else if (age1 > 13 && age1 <= 18)
  81. //{
  82. //    Console.WriteLine("You are teenager!");
  83. //}
  84. //else
  85. //{
  86. //    Console.WriteLine("You are an aduld");
  87. //}
  88.  
  89. //Console.Write("Enter the first letter of your name: ");
  90. //char firstLetter = char.Parse(Console.ReadLine());
  91.  
  92. //switch (firstLetter)
  93. //{
  94. //    case 'S':
  95. //        Console.WriteLine("You are a lucki man");
  96. //        break;
  97. //    case 'D':
  98. //        Console.WriteLine("You are a great man");
  99. //        break;
  100. //    default:
  101. //        Console.WriteLine("I don't know what to say");
  102. //        break;
  103. //}
  104.  
  105. // 6. Loops
  106. // 6.1. Print the number from 1 to 100
  107. for (int i = 0; i < 10; i++)
  108. {
  109.     Console.WriteLine(i);
  110. }
  111.  
  112. // II Second day of Excercises
  113. //7. Arrays
  114. // 7.1. Create an array of integers double each one of the values and print it.
  115. // Declare an array of 10 Ints assign values and print them on the console
  116. string[] namesOfStudents = new string[] {
  117.     "Yaren",
  118.     "Darina",
  119.     "Aishe",
  120.     "Suzana",
  121.     "Momchil",
  122.     "Ivan...14",
  123.     "Stanislava",
  124.     "Ilker",
  125.     "Ilkay",
  126.     "Dean",
  127.     "Nikola"
  128. };
  129.  
  130. Console.WriteLine("Hello Deny!");
  131.  
  132. int[] numbers = new int[10];
  133. numbers[0] = 10;
  134. numbers[1] = 12;
  135.  
  136. foreach (var n in numbers)
  137. {
  138.     Console.WriteLine(n);
  139. }
  140. Greet("Stanislava");
  141. int number;
  142. do
  143. {
  144.     Console.Write("Please enter a number between 1 - 5: ");
  145.     number = int.Parse(Console.ReadLine());
  146.  
  147. } while (number < 1 || number > 5);
  148.  
  149. // 8. Number systems
  150. Console.WriteLine("2093 to BIN {0}.", Convert.ToString(2093, 2));
  151. Console.WriteLine("2093 to OCT {0}.", Convert.ToString(2093, 8));
  152. Console.WriteLine("2093 to HEX {0}.", Convert.ToString(2093, 16));
  153.  
  154. // 9. Methods
  155. // 9.1. define method fibonachi that prints the n-th number of Fibbonachi secuence. ANd print the first 100 fibonachies.
  156. // Declare and define a method which grrets. First without formal parameters and returned value. After that with a formal parameter
  157. Console.WriteLine(AddNumbers(34, 54));
  158. static void Greet(string name = "Deny")
  159. {
  160.     Console.WriteLine($"Hello {name}!");
  161. }
  162.  
  163. // Declare and define a function with a returned value which adds two digits
  164. static int AddNumbers(int a, int b)
  165. {
  166.     return a + b;
  167. }
  168.  
  169. static bool IsOdd(int num)
  170. {
  171.     return (num % 2 == 1)? true : false;
  172. }
  173.  
  174. // 10. Recursion
  175. // 10.1. Change the fibbonachi from 9.1. to recursion and do the same print the first 100 fibonnachi numbers
  176. // With the help of ChatGPT create a function factorial
  177. // factorial
  178. // 0! = 1
  179. // n! = (n-1)! * n
  180. static int Factorial(int n)
  181. {
  182.     if (n == 0)
  183.     {
  184.         return 1;
  185.     }
  186.     else
  187.     {
  188.         return n * Factorial(n - 1);
  189.     }
  190. }
  191.  
  192.  
  193. Console.WriteLine(Factorial(3));
  194.  
  195. static double FactorialIter(int n)
  196. {
  197.     double factorial = 1;
  198.     for (int i = 2; i <= n; i++)
  199.     {
  200.         factorial *= i;
  201.     }
  202.     return factorial;
  203. }
  204.  
  205. // III. Excercise
  206. // 1. Define a function that finds the min element in array of ints
  207.  
  208.  
  209. //int[] nums = {1,2,3,4,5,6,7,8,9};
  210.  
  211. //void FindLowest()
  212. //{
  213. //    int lowest = int.MaxValue;
  214. //    for (int i = 0; i < nums.Length; i++)
  215. //    {
  216. //        int current = nums[i];
  217. //        if (current < lowest)
  218. //        {
  219. //            current = lowest;
  220. //        }
  221. //    }
  222. //    Console.WriteLine( lowest);
  223. //};
  224. //FindLowest();
  225.  
  226.  
  227. // Min
  228. static int FindMin(int[] nArray)
  229. {
  230.     int min = nArray[0];
  231.     foreach (int num in nArray)
  232.     {
  233.         if (min > num) { min = num; }
  234.     }
  235.     return min;
  236. }
  237.  
  238. // Max
  239. static int FindMax(int[] nArray)
  240. {
  241.     int max = nArray[0];
  242.     foreach (int num in nArray)
  243.     {
  244.         if (max < num) { max = num; }
  245.     }
  246.     return max;
  247. }
  248.  
  249. // Sort
  250. static void BubbleSortDescending(int[] arr)
  251. {
  252.     int temp;
  253.     for (int i = 0; i < arr.Length - 1; i++)
  254.     {
  255.         for (int j = 0; j < arr.Length - i - 1; j++)
  256.         {
  257.             if (arr[j] < arr[j + 1]) // For ascending order, use '>'
  258.             {
  259.                 // Swap the elements
  260.                 temp = arr[j];
  261.                 arr[j] = arr[j + 1];
  262.                 arr[j + 1] = temp;
  263.             }
  264.         }
  265.     }
  266. }
  267.  
  268. // Filter
  269. static int[] FilterOddNumbers(int[] arr)
  270. {
  271.     int count = 0;
  272.     for (int i = 0; i < arr.Length; i++)
  273.     {
  274.         if (arr[i] % 2 != 0)
  275.         {
  276.             count++;
  277.         }
  278.     }
  279.  
  280.     int[] result = new int[count];
  281.     int index = 0;
  282.     for (int i = 0; i < arr.Length; i++)
  283.     {
  284.         if (arr[i] % 2 != 0)
  285.         {
  286.             result[index] = arr[i];
  287.             index++;
  288.         }
  289.     }
  290.  
  291.     return result;
  292. }
  293.  
  294.  
  295. int[] nums = { 23, 29, 354, 432, 765, 89, 90, 87, 9, 10 };
  296. Console.WriteLine("Find Min = {0}", FindMin(nums));
  297. Console.WriteLine("Find Min = {0}", FindMax(nums));
  298.  
  299.  
  300. // int[] oddNums = nums.Where(n => n % 2 != 0).ToArray();
  301. int[] oddNums = FilterOddNumbers(nums);
  302. foreach (var num in oddNums)
  303. {
  304.     Console.WriteLine(num);
  305. }
  306.  
  307. BubbleSortDescending(nums);
  308.  
  309. // Print the sorted array
  310. foreach (var num in nums)
  311. {
  312.     Console.WriteLine(num);
  313. }
Advertisement
Add Comment
Please, Sign In to add comment