Statistics - C# Programming Exercise

This C# exercise aims to develop a program that allows the user to input several numbers and calculate basic statistical operations such as sum, average, minimum, and maximum of those numbers. The program will keep adding the numbers entered and display these values at each step. Furthermore, the program will terminate when the user enters the number 0, causing the farewell message to appear on the screen. The use of accumulative variables, conditional structures, and loops is essential in this exercise to gather data and perform calculations on each iteration. This exercise is useful for learning how to handle user input, as well as managing conditions and calculations within a loop until a termination condition is met. Additionally, it helps practice how to calculate the average and find the minimum and maximum values in a set of data entered by the user.

 Category

Flow Control

 Exercise

Statistics

 Objective

Write a C# program to calculate various basic statistical operations: it will accept numbers from the user and display their sum, average, minimum and maximum, as in the following example:

Number? 5
Total=5 Count=1 Average=5 Max=5 Min=5

Number? 2
Total=7 Count=2 Average=3.5 Max=5 Min=2

Number? 0
Goodbye!

(As seen in this example, the program will end when the user enters 0)

 Write Your C# Exercise

using System; // Importing the System namespace to use Console functionalities

class Program
{
    // Main method where the program execution begins
    static void Main()
    {
        double total = 0; // Variable to store the total sum of numbers
        int count = 0;    // Variable to store the count of entered numbers
        double max = double.MinValue; // Initialize max to the smallest possible value
        double min = double.MaxValue; // Initialize min to the largest possible value

        while (true)
        {
            // Prompt the user to enter a number
            Console.Write("Number? ");
            double number = double.Parse(Console.ReadLine()); // Read the number and parse it to double

            // If the user enters 0, break out of the loop and end the program
            if (number == 0)
            {
                Console.WriteLine("Goodbye!");
                break;
            }

            // Update total, count, max, and min
            total += number;
            count++;
            if (number > max)
            {
                max = number; // Update max if the current number is greater
            }
            if (number < min)
            {
                min = number; // Update min if the current number is smaller
            }

            // Calculate the average
            double average = total / count;

            // Display the statistics
            Console.WriteLine($"Total={total} Count={count} Average={average} Max={max} Min={min}");
        }
    }
}

 Share this C# exercise

 More C# Programming Exercises of Flow Control

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#.

  •  Switch

    This C# exercise aims to develop a program that, given a numerical grade, displays the corresponding text grade according to the following equivalence: 9 and ...

  •  Conditional operator, positive & smaller

    This C# exercise aims to develop a program that asks the user for two numbers and uses the conditional operator (?) to answer the following questions: ...

  •  Prime number

    This C# exercise aims to develop a program that asks the user for an integer and determines if it is a prime number or not. A prime number is one...

  •  Give change

    This exercise in C# aims to develop a program that calculates the change for a purchase, using the largest possible coins or bills. The program s...

  •  Exceptions

    This exercise in C# aims to develop a program that asks the user for two numbers and displays their division. The program should handle potential errors...

  •  Positive and negative

    In this C# exercise, you will learn how to create a program that determines whether a number entered by the user is positive or negative. The program will prom...