Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Exercises
- // I. first day
- // 1. Hello World
- using System.Security.Cryptography.X509Certificates;
- Greet("Ilkay");
- // 2. Variables, Primitive data Types Literals
- Console.WriteLine("Test");
- int a = 7;
- int b = 3;
- int speedOfLight = 0xe3; //300000;
- Console.WriteLine((float)a/b);
- // 3. Operators
- Console.WriteLine(a*b > b*b*a || b + a < a*b);
- // 4. Formatted output/ Reading from console
- Console.WriteLine("{0,10:C2}", b);
- Console.Write("Please, enter your age: ");
- string age = Console.ReadLine();
- int ageDigit = int.Parse(age);
- Console.WriteLine("In 10 years you will be {0}", ageDigit + 10);
- // Console.Write("Please, tell me how much money do you have: ");
- // 5. Conditional Logic
- //Console.Write("Please enter your age: ");
- //string aaa = Console.ReadLine();
- //int age1 = int.Parse(Console.ReadLine());
- ////if (age1 >= 0 && age1 <= 3)
- ////{
- //// Console.WriteLine("You are a baby");
- ////}
- //if (age1 >= 0 && age1 <=3)
- //{
- // Console.WriteLine("You are a baby");
- //}
- //else if (age1 >= 3 && age1 <= 13)
- //{
- // Console.WriteLine("You are a kid");
- //}
- //else if (age1 > 13 && age1 <= 18)
- //{
- // Console.WriteLine("You are teenager!");
- //}
- //else
- //{
- // Console.WriteLine("You are an aduld");
- //}
- //Console.Write("Enter the first letter of your name: ");
- //char firstLetter = char.Parse(Console.ReadLine());
- //switch (firstLetter)
- //{
- // case 'S':
- // Console.WriteLine("You are a lucki man");
- // break;
- // case 'D':
- // Console.WriteLine("You are a great man");
- // break;
- // default:
- // Console.WriteLine("I don't know what to say");
- // break;
- //}
- // 6. Loops
- for (int i = 0; i < 10; i++)
- {
- Console.WriteLine(i);
- }
- // II Second day of Excercises
- //7. Arrays
- // Declare an array of 10 Ints assign values and print them on the console
- string[] namesOfStudents = new string[] {
- "Yaren",
- "Darina",
- "Aishe",
- "Suzana",
- "Momchil",
- "Ivan...14",
- "Stanislava",
- "Ilker",
- "Ilkay",
- "Dean",
- "Nikola"
- };
- Console.WriteLine("Hello Deny!");
- int[] numbers = new int[10];
- numbers[0] = 10;
- numbers[1] = 12;
- foreach (var n in numbers)
- {
- Console.WriteLine(n);
- }
- Greet("Stanislava");
- int number;
- do
- {
- Console.Write("Please enter a number between 1 - 5: ");
- number = int.Parse(Console.ReadLine());
- } while (number < 1 || number > 5);
- // 8. Number systems
- Console.WriteLine("2093 to BIN {0}.", Convert.ToString(2093, 2));
- Console.WriteLine("2093 to OCT {0}.", Convert.ToString(2093, 8));
- Console.WriteLine("2093 to HEX {0}.", Convert.ToString(2093, 16));
- // 9. Methods
- // Declare and define a method which grrets. First without formal parameters and returned value. After that with a formal parameter
- Console.WriteLine(AddNumbers(34, 54));
- static void Greet(string name = "Deny")
- {
- Console.WriteLine($"Hello {name}!");
- }
- // Declare and define a function with a returned value which adds two digits
- static int AddNumbers(int a, int b)
- {
- return a + b;
- }
- static bool IsOdd(int num)
- {
- return (num % 2 == 1)? true : false;
- }
- // 10. Recursion
- // With the help of ChatGPT create a function factorial
- // factorial
- // 0! = 1
- // n! = (n-1)! * n
- static int Factorial(int n)
- {
- if (n == 0)
- {
- return 1;
- }
- else
- {
- return n * Factorial(n - 1);
- }
- }
- Console.WriteLine(Factorial(3));
- static double FactorialIter(int n)
- {
- double factorial = 1;
- for (int i = 2; i <= n; i++)
- {
- factorial *= i;
- }
- return factorial;
- }
- // III. Excercise
- // 1. Define a function that finds the min element in array of ints
- //int[] nums = {1,2,3,4,5,6,7,8,9};
- //void FindLowest()
- //{
- // int lowest = int.MaxValue;
- // for (int i = 0; i < nums.Length; i++)
- // {
- // int current = nums[i];
- // if (current < lowest)
- // {
- // current = lowest;
- // }
- // }
- // Console.WriteLine( lowest);
- //};
- //FindLowest();
- // Min
- static int FindMin(int[] nArray)
- {
- int min = nArray[0];
- foreach (int num in nArray)
- {
- if (min > num) { min = num; }
- }
- return min;
- }
- // Max
- static int FindMax(int[] nArray)
- {
- int max = nArray[0];
- foreach (int num in nArray)
- {
- if (max < num) { max = num; }
- }
- return max;
- }
- // Sort
- static void BubbleSortDescending(int[] arr)
- {
- int temp;
- for (int i = 0; i < arr.Length - 1; i++)
- {
- for (int j = 0; j < arr.Length - i - 1; j++)
- {
- if (arr[j] < arr[j + 1]) // For ascending order, use '>'
- {
- // Swap the elements
- temp = arr[j];
- arr[j] = arr[j + 1];
- arr[j + 1] = temp;
- }
- }
- }
- }
- // Filter
- static int[] FilterOddNumbers(int[] arr)
- {
- int count = 0;
- for (int i = 0; i < arr.Length; i++)
- {
- if (arr[i] % 2 != 0)
- {
- count++;
- }
- }
- int[] result = new int[count];
- int index = 0;
- for (int i = 0; i < arr.Length; i++)
- {
- if (arr[i] % 2 != 0)
- {
- result[index] = arr[i];
- index++;
- }
- }
- return result;
- }
- int[] nums = { 23, 29, 354, 432, 765, 89, 90, 87, 9, 10 };
- Console.WriteLine("Find Min = {0}", FindMin(nums));
- Console.WriteLine("Find Min = {0}", FindMax(nums));
- // int[] oddNums = nums.Where(n => n % 2 != 0).ToArray();
- int[] oddNums = FilterOddNumbers(nums);
- foreach (var num in oddNums)
- {
- Console.WriteLine(num);
- }
- BubbleSortDescending(nums);
- // Print the sorted array
- foreach (var num in nums)
- {
- Console.WriteLine(num);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement