Write A Program To Find Numbers Above And Below The Average In C#

In this C# tutorial, I will show you how to write a program to find numbers above and below the average in C#.

To find numbers above and below the average in a C# program, calculate the average of a number list and then iterate through the list, comparing each number with the average. Use two separate methods: one to calculate the average and another to display numbers based on whether they are above or below this average. This approach offers a clear and efficient solution for data analysis tasks in C#.

Write A Program To Find Numbers Above And Below The Average In C#

Here, we have a collection of numbers, and we need to find the average of these numbers. Once we have the average, we aim to determine which numbers are above and below this average in C#.

In our C# program, first, we need to sum all the numbers in the collection and divide by the total count and then iterate through the collection and compare each number with the average using the C# code.

Here is the complete C# code:

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<double> numbers = new List<double> { 12, 55, 74, 18, 65, 88, 43 };
        double average = CalculateAverage(numbers);

        Console.WriteLine("Average: " + average);
        Console.WriteLine("Numbers Above Average: ");
        DisplayNumbersAboveOrBelowAverage(numbers, average, true);
        Console.WriteLine("Numbers Below Average: ");
        DisplayNumbersAboveOrBelowAverage(numbers, average, false);
    }

    static double CalculateAverage(List<double> numbers)
    {
        double sum = 0;
        foreach (double num in numbers)
        {
            sum += num;
        }
        return sum / numbers.Count;
    }

    static void DisplayNumbersAboveOrBelowAverage(List<double> numbers, double average, bool above)
    {
        foreach (double num in numbers)
        {
            if (above && num > average) Console.WriteLine(num);
            else if (!above && num < average) Console.WriteLine(num);
        }
    }
}
  • CalculateAverage Method: This method takes a list of doubles as an argument, calculates the sum, and returns the average.
  • DisplayNumbersAboveOrBelowAverage Method: This method displays numbers from the list based on whether they are above or below the average. The ‘above’ boolean parameter determines which set of numbers to display.
  • Main Method: We create a list of doubles, calculate the average, and then call the method to display numbers above and below the average.

Once you run the code using a console application using Visual Studio, you can see the output, like the screenshot below.

Numbers Above Average:
55
74
65
88
Numbers Below Average:
12
18
43
Write a program to find numbers above and below the average in C#

Conclusion

With this simple yet powerful C# program, you can effectively find numbers above and below the average in a given list. This program is a great example of how C# can be used for data analysis and manipulation. I hope you got an idea of how to find numbers above and below the average in C# from this program.

You may also like: