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
C# Exercise Example
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.");
}
}
}
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#.
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...
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...
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 ...
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)
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...
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...