
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to compute Average for the Set of Values using C#?
Firstly, set an array with values −
int[] myArr = new int[] { 34, 23, 77, 67 };
To get the average, firstly get the sum of array elements.
Divide the sum with the length of the array and that will give you the average of the elements −
int sum = 0; int average = 0; for (int i = 0; i < len; i++) { sum += myArr[i]; } average = sum / len;
The following is the complete code to get the array in C# −
Example
using System; public class Program { public static void Main() { int[] myArr = new int[] { 34, 23, 77, 67 }; int len = myArr.Length; int sum = 0; int average = 0; for (int i = 0; i < len; i++) { sum += myArr[i]; } average = sum / len; Console.WriteLine("Average = " + average); } }
Output
Average = 50
- Related Articles
- How to find the Average Values of all the Array Elements in C#?
- How to set only numeric values for edittext in Android using Kotlin?
- C++ Program to Compute Combinations using Recurrence Relation for nCr
- How to Average the Absolute Values in Excel?
- C# Program to find the average of a sequence of numeric values
- How to get the possible values for SET field in MySQL?
- How to compute the area of a set of bounding boxes in PyTorch?
- Find the average of column values in MySQL using aggregate function
- C++ Program to Compute the Area of a Triangle Using Determinants
- C++ Program to Compute Combinations using Factorials
- How to Set/modify the pixels(RGB values) of an image using Java OpenCV Library?
- How to set values to list of parameters of IN clause on PreparedStatement using JDBC?
- How to find the average of non-zero values in a Python dictionary?
- C++ Program to Calculate Average of Numbers Using Arrays
- How to fetch fields with multiple values set using MySQL LIKE?

Advertisements