Write a program that requests the user for 20 numbers and calculates the average of the numbers which are smaller than 10. You have to make use of a loop.
using System;
class ConsoleApp1
{
static void Main()
{
const int n = 20;
Console.WriteLine("Enter 20 numbers:");
double sum = 0;
int count = 0;
for (int i = 0; i < n; i++)
{
string s = "";
double d = 0;
while (!Double.TryParse(s, out d))
{
Console.Write(">> ");
s = Console.ReadLine();
}
if (d < 10)
{
sum += d;
count++;
}
}
Console.WriteLine($"Average of the numbers which are smaller than 10: {sum/count:0.0000}");
}
}
Comments
Leave a comment