In this example, you will learn how to count the numbers in array, which are below than average number and above than average number.
C# Code:
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 | static void Main(string[] args) { int counter = 0; int[] numbers = new int[10]; int sum=0,avg=0,low=0,high=0; for(int i=0;i<10;i++) { Console.Write("Number {0}: ",(i+1)); numbers[i] = Convert.ToInt32(Console.ReadLine()); sum += numbers[i]; } avg = sum / 10; //avg = sum / numbers.Length; for (int i=0;i<10;i++) { if(numbers[i]<avg) { low++; } else { high++; } } Console.WriteLine("The average is : {0}", avg); Console.WriteLine("The numbers above the average are: {0}", high); Console.WriteLine("The numbers below the average are: {0}", low); Console.ReadKey(); } |
Output: