Array Of Positive And Negative Numbers - C# Programming Exercise

In this C# exercise, you are asked to write a program that asks the user for 10 real numbers and displays the average of the positive ones and the average of the negative ones.

This exercise will help you practice handling real numbers, using conditions, and loops to process a list of data provided by the user. The program should analyze the 10 numbers entered, separate them into two groups (positive and negative), and calculate the average of each group.

Additionally, this exercise is great for improving data manipulation in C#, especially when it comes to working with floating-point numbers and managing control flow with conditional structures and loops.

 Category

Arrays, Structures and Strings

 Exercise

Array Of Positive And Negative Numbers

 Objective

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.

 Write Your C# Exercise

using System;  // Import the System namespace for basic functionality

class Program  // Define the main class
{
    static void Main()  // The entry point of the program
    {
        // Declare an array to store 10 real numbers
        double[] numbers = new double[10];
        
        // Variables to store the sum and count of positive and negative numbers
        double positiveSum = 0, negativeSum = 0;
        int positiveCount = 0, negativeCount = 0;

        // Ask the user to input 10 real numbers
        Console.WriteLine("Please enter 10 real numbers:");

        // Loop to input 10 numbers
        for (int i = 0; i < 10; i++)
        {
            Console.Write($"Enter number {i + 1}: ");
            numbers[i] = Convert.ToDouble(Console.ReadLine());  // Store each number in the array
        }

        // Calculate the sum and count for positive and negative numbers
        foreach (double num in numbers)
        {
            if (num > 0)  // Check if the number is positive
            {
                positiveSum += num;
                positiveCount++;
            }
            else if (num < 0)  // Check if the number is negative
            {
                negativeSum += num;
                negativeCount++;
            }
        }

        // Display the average of the positive numbers, if there are any
        if (positiveCount > 0)
        {
            double positiveAverage = positiveSum / positiveCount;
            Console.WriteLine($"The average of the positive numbers is: {positiveAverage}");
        }
        else
        {
            Console.WriteLine("There are no positive numbers.");
        }

        // Display the average of the negative numbers, if there are any
        if (negativeCount > 0)
        {
            double negativeAverage = negativeSum / negativeCount;
            Console.WriteLine($"The average of the negative numbers is: {negativeAverage}");
        }
        else
        {
            Console.WriteLine("There are no negative numbers.");
        }
    }
}

 Share this C# exercise

 More C# Programming Exercises of Arrays, Structures and Strings

Explore our set of C# programming 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#.

  •  Many numbers and sum

    In this C# exercise, you are asked to write a program that asks the user for several numbers (until they enter the word "end") and then displays the sum of the...

  •  Two dimensional array

    In this C# exercise, you are asked to write a program that asks the user for marks for 20 pupils (2 groups of 10, using a two-dimensional array), and then disp...

  •  Statistics V2

    In this C# exercise, you are asked to create a statistical program that allows the user to perform the following actions: - Add new data - See all data ...

  •  Struct

    In this C# exercise, you are asked to create a struct to store data of 2D points. The fields for each point will be: - x coordinate (short)

  •  Array of struct

    In this C# exercise, you are asked to expand the previous exercise that used a struct to store 2D points. Now, you need to store up to 1,000 points usin...

  •  Array of struct and menu

    In this C# exercise, you are asked to expand the previous exercise (array of points) so that the program displays an interactive menu. The menu should a...