In statistics, the standard deviation is a measure of the amount of variation or dispersion of a set of values. In this task, we will write a C# program to find the standard deviation of a set of numbers.
Problem Statement
Write a C# program to find the standard deviation of a set of numbers. The program should take input from the user in the form of a set of numbers. The program should display the standard deviation of the numbers as output.
C# Program to Find the Standard Deviation
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main(string[] args) { Console.WriteLine("Enter the numbers separated by spaces: "); string input = Console.ReadLine(); string[] numbers = input.Split(' '); List<double> values = new List<double>(); foreach (string number in numbers) { values.Add(double.Parse(number)); } double mean = values.Average(); double sumOfSquares = 0; foreach (double value in values) { sumOfSquares += Math.Pow(value - mean, 2); } double standardDeviation = Math.Sqrt(sumOfSquares / values.Count); Console.WriteLine("Standard Deviation: {0}", standardDeviation); } }
C#
x
29
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
5
class Program
6
{
7
static void Main(string[] args)
8
{
9
Console.WriteLine("Enter the numbers separated by spaces: ");
10
string input = Console.ReadLine();
11
string[] numbers = input.Split(' ');
12
13
List<double> values = new List<double>();
14
foreach (string number in numbers)
15
{
16
values.Add(double.Parse(number));
17
}
18
19
double mean = values.Average();
20
double sumOfSquares = 0;
21
foreach (double value in values)
22
{
23
sumOfSquares += Math.Pow(value - mean, 2);
24
}
25
26
double standardDeviation = Math.Sqrt(sumOfSquares / values.Count);
27
Console.WriteLine("Standard Deviation: {0}", standardDeviation);
28
}
29
}
Input / Output
