Answer to Question #312721 in C# for kun

Question #312721

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.


1
Expert's answer
2022-03-17T13:56:01-0400
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}");
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS