Average - C# Programming Exercise

This exercise is a great opportunity to learn how to calculate the average of several numbers in C#. In this program, the user must enter four numbers, and the program will calculate and display the average of those numbers. This type of exercise will help you understand how to perform simple arithmetic operations in C# and how to handle user inputs to calculate specific results.

The instructions for the user are as follows:
1. The program will prompt the user to enter four numbers.
2. Then, it will calculate the average of those numbers and display the result on the screen.

This exercise is perfect for practicing how to use Console.ReadLine() to capture user input and how to perform arithmetic operations in C# such as summing numbers and dividing to get the average. You'll also learn how to display results using Console.WriteLine(), one of the most essential tools for interacting with the user in the console.

 Category

First contact with C# Sharp

 Exercise

Average

 Objective

Write a C# program to calculate and display the average of four numbers entered by the user.

 Write Your C# Exercise

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

// Main class of the program
class Program
{
    // Main method where the program execution begins
    static void Main()
    {
        // Declaring four variables to store the numbers entered by the user
        double num1, num2, num3, num4;

        // Asking the user to enter the first number and reading the input
        Console.Write("Enter the first number: ");
        num1 = Convert.ToDouble(Console.ReadLine());

        // Asking the user to enter the second number and reading the input
        Console.Write("Enter the second number: ");
        num2 = Convert.ToDouble(Console.ReadLine());

        // Asking the user to enter the third number and reading the input
        Console.Write("Enter the third number: ");
        num3 = Convert.ToDouble(Console.ReadLine());

        // Asking the user to enter the fourth number and reading the input
        Console.Write("Enter the fourth number: ");
        num4 = Convert.ToDouble(Console.ReadLine());

        // Calculating the average of the four numbers
        double average = (num1 + num2 + num3 + num4) / 4;

        // Printing the average to the screen
        Console.WriteLine("The average of the four numbers is: " + average);
    }
}

 Share this C# exercise

 More C# Programming Exercises of First contact with C# Sharp

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

  •  Equivalent operations

    This C# exercise is perfect for learning how to perform mathematical operations with three numbers provided by the user. In this program, the user must enter three numbers: ...

  •  Age

    This C# exercise is perfect for learning how to interact with the user and use input data to personalize the program's output. In this case, the program will ask the user to...

  •  Formats

    This C# exercise teaches you how to handle console output using two different methods: Console.Write and formatting with {0}. In this program, the user will in...

  •  Rectangle

    This C# exercise helps you practice using loops and user input. In this program, the user will be asked to input a number (a digit), and the program will display a re...

  •  Conversion

    This C# exercise teaches you how to perform temperature unit conversions. The program prompts the user to input a temperature in Celsius degrees and then converts it ...

  •  First contact with C#

    This exercise is an excellent way to get started with programming in C#. In this program, you will learn how to print a message on the screen using C#. The program wi...