Calculating the Average of Positive and Negative Numbers in C#

This exercise focuses on working with arrays, loops, and conditional statements in C#. The program will ask the user to enter 10 real numbers (floating-point), store them in an array, and calculate the average of the positive numbers separately from the average of the negative numbers. The program will display both results. This helps practice user input handling, arithmetic operations, and conditional logic.



Group

C# Arrays, Structures and Strings

Objective

1. Ask the user to enter 10 real numbers.
2. Store these numbers in an array.
3. Separate the positive and negative numbers while iterating through the array.
4. Calculate the average of the positive numbers.
5. Calculate the average of the negative numbers.
6. Display both results on the screen.
7. Ensure proper handling for cases where there are no positive or negative numbers.

Write a C# program to ask the user for 10 real numbers and display the average of the positive ones and the average of the negative ones.

Example C# Exercise

 Copy C# Code
using System;

class Program
{
    static void Main()
    {
        // Define an array to store 10 real numbers
        double[] numbers = new double[10];

        // Variables to store sums and counts
        double sumPositive = 0, sumNegative = 0;
        int countPositive = 0, countNegative = 0;

        // Prompt the user to enter 10 real numbers
        Console.WriteLine("Enter 10 real numbers:");

        for (int i = 0; i < numbers.Length; i++)
        {
            Console.Write($"Number {i + 1}: ");
            numbers[i] = Convert.ToDouble(Console.ReadLine());

            // Categorize numbers as positive or negative
            if (numbers[i] > 0)
            {
                sumPositive += numbers[i];
                countPositive++;
            }
            else if (numbers[i] < 0)
            {
                sumNegative += numbers[i];
                countNegative++;
            }
        }

        // Calculate averages
        double avgPositive = countPositive > 0 ? sumPositive / countPositive : 0;
        double avgNegative = countNegative > 0 ? sumNegative / countNegative : 0;

        // Display results
        Console.WriteLine("\nResults:");
        Console.WriteLine(countPositive > 0 ? $"Average of positive numbers: {avgPositive:F2}" : "No positive numbers entered.");
        Console.WriteLine(countNegative > 0 ? $"Average of negative numbers: {avgNegative:F2}" : "No negative numbers entered.");
    }
}

 Output

Enter 10 real numbers:
Number 1: 4.5
Number 2: -3.2
Number 3: 7.8
Number 4: -1.1
Number 5: 0
Number 6: 6.2
Number 7: -2.5
Number 8: 8.4
Number 9: -4.6
Number 10: 3.3

Results:
Average of positive numbers: 6.04
Average of negative numbers: -2.85

Share this C# Exercise

More C# Practice Exercises of C# Arrays, Structures and Strings

Explore our set of C# Practice Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.