1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
static void Main() { double sum=0, avg=0; double[] numbers = { 10, 20, 50, 40}; for(int i=0;i<numbers.Length;i++) { sum += numbers[i]; } avg = sum / numbers.Length; Console.WriteLine("The Sum is : "+sum); Console.WriteLine("The Average is : "+avg); Console.ReadKey(); } |
Найти второе по величине число в массиве, используя C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
static void Main(string[] args) { int n, i, j = 0, largest, secondLargest; int[] arr1 = new int[50]; Console.Write("\n\nFind the second largest element in an array :\n"); Console.Write("-----------------------------------------\n"); Console.Write("Input the size of array : "); n = Convert.ToInt32(Console.ReadLine()); /* Stored values into the array*/ Console.Write("Input {0} elements in the array :\n", n); for (i = 0; i < n; i++) { Console.Write("element - {0} : ", i); arr1[i] = Convert.ToInt32(Console.ReadLine()); } /* find location of the largest element in the array */ largest = 0; for (i = 0; i < n; i++) { if (largest < arr1[i]) { largest = arr1[i]; j = i; } } /* ignore the largest element and find the 2nd largest element in the array */ secondLargest = 0; for (i = 0; i < n; i++) { if (i == j) { i++; /* ignoring the largest element */ i--; } else { if (secondLargest < arr1[i]) { secondLargest = arr1[i]; } } } Console.Write("The Second largest element in the array is : {0} \n\n", secondLargest); Console.ReadKey(); } |
Алгоритм преобразования десятичной дроби в двоичную в языке C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
static void Main(string[] args) { int number; Console.Write("Enter a Number : "); number = int.Parse(Console.ReadLine()); int q; string rem = ""; while (number >= 1) { q = number / 2; rem += (number % 2).ToString(); number = q; } string binary = ""; for (int i = rem.Length - 1; i >= 0; i--) { binary = binary + rem[i]; } Console.WriteLine("The Binary format for {0} is {1}",number, bi |
Проверка наличия строк или чисел палиндрома в C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
static void Main(string[] args) { Console.Write("Enter something for to check that is it palindrome :"); string text = Console.ReadLine(); int len = text.Length; bool flag = true; //check palindrome for (int i = 0; i < len/2; i++) { if (text[i] != text[len - (i + 1)]) { flag = false; break; } } //if flag true, text is palindrome if (flag) { Console.WriteLine("{0} is palindrome", text); } else { Console.WriteLine("{0} is not palindrome", text); } Console.ReadLine(); } |
1
Отображение числа Армстронга между двумя интервалами в C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
static void Main(string[] args) { int num1,num2, n, sum, r; Console.Write("Enter positive number1 :"); num1 = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter positive number2 :"); num2 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Armstrong Number from {0} to {1}",num1,num2); for (int i = num1; i <= num2; i++) { sum = 0; n = i; while (n != 0) { r = n % 10; sum = sum + (r * r * r); n = n / 10; } if (i == sum) Console.WriteLine(i); } Console.ReadKey(); } |