Basic Programming C# Examples

C# Program to Print the Average of Given Input

The Program in C# Program to Print the Average of the Given Input is given below:

using System;

namespace AverageCalculator
{
    class Program
    {
        static void Main(string[] args)
        {
          Console.WriteLine("Hello Codeauri Family, Enter the  number here to find their average: ");
            int count = Convert.ToInt32(Console.ReadLine());
            int[] numbers = new int[count];

            Console.WriteLine("Enter " + count + " numbers:");
            for (int i = 0; i < count; i++)
            {
                numbers[i] = Convert.ToInt32(Console.ReadLine());
            }

            int sum = 0;
            foreach (int num in numbers)
            {
                sum += num;
            }

            double average = (double)sum / count;
            Console.WriteLine("Okay, the average is: " + average);
            Console.ReadLine();
        }
    }
}

Output:

Hello Codeauri Family, Enter the number here to find their average:
3

Enter 3 numbers:
20550420
20560423
20560804
Okay, the average is: 20557215.6666667

Pro-Tips💡

Here are the step-by-step execution of above code:

  1. using System; is a directive that imports the System namespace, which contains various classes and methods used in the program.
  2. namespace AverageCalculator defines a namespace that contains all the classes and methods within it.
  3. class Program defines a class that contains the program’s logic.
  4. static void Main(string[] args) is the program’s entry point, which is executed when the program starts.
  5. Console.WriteLine("Hello Codeauri Family, Enter the number here to find their average: "); prints a prompt asking the user to enter the number of numbers they want to find the average of.
  6. int count = Convert.ToInt32(Console.ReadLine()); reads the user’s input, converts it from a string to an integer using Convert.ToInt32, and stores the result in the variable count.
  7. int[] numbers = new int[count]; creates an array of integers with count elements.
  8. Console.WriteLine("Enter " + count + " numbers:"); prints a prompt asking the user to enter count numbers.
  9. for (int i = 0; i < count; i++) is a for loop that will iterate count times. i is the loop variable that starts from 0 and increments by 1 each time through the loop.
  10. numbers[i] = Convert.ToInt32(Console.ReadLine()); reads a number from the user, converts it from a string to an integer using Convert.ToInt32, and stores the result in the numbers array.
  11. int sum = 0; declares a variable sum to store the sum of the numbers.
  12. foreach (int num in numbers) is a foreach loop that iterates over each element in the numbers array. num is the loop variable that holds the current element.
  13. sum += num; adds the current element to the sum.
  14. double average = (double)sum / count; calculates the average by dividing the sum by the count and converts the result to a double.
  15. Console.WriteLine("Okay, the average is: " + average); prints the average.
  16. Console.ReadLine(); waits for the user to press the Enter key before ending the program.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X