- Course
- Arrays
- Search positive and negative numbers
Search positive and negative numbers
Last updated:
8/23/2020
⁃
Difficulty:
Intermediate
Create a C# program that asks the user for 10 real numbers and shows the arithmetic mean of the positive and negative numbers.
Input
3
-5
2
4
-3
1
8
-10
6
4
Output
Average numbers negatives is -6
Average numbers positives is 4
Solution
using System;
public class SearchPositiveAndNegativeNumbers
{
public static void Main(string[] args)
{
int total = 10;
float[] numbers = new float[total];
float totalPositive = 0.0f, totalNegative = 0.0f;
int countPositive = 0, countNegative = 0;
for (int i = 0; i < total; i++)
{
numbers[i] = Convert.ToSingle(Console.ReadLine());
}
for (int i = 0; i < total; i++)
{
if (numbers[i] < 0) // Negative
{
totalNegative = totalNegative + numbers[i];
countNegative++;
}
if (numbers[i] > 0) // Positive
{
totalPositive = totalPositive + numbers[i];
countPositive++;
}
}
Console.WriteLine("Average numbers negatives is {0}", totalNegative / countNegative);
Console.WriteLine("Average numbers positives is {0}", totalPositive / countPositive);
}
}