C# Program to Find the Standard Deviation

This is a C# Program to find the standard deviation of a set of given numbers.

Problem Description

This C# Program Finds the Standard Deviation of a Set of Given Numbers.

Problem Solution

Here the mean, variance and standard deviation are calculated and displayed.

Program/Source Code

Here is source code of the C# Program to Find the Standard Deviation of a Set of Given Numbers. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Find the Standard Deviation of a Set of Given Numbers
 */
using System;
using System.Collections.Generic;
namespace SampleApp
{
    internal class Program
    {
        private static void Main()
        {
            List<double> number = new List<double> { 1, 2, 3, 4, 5, 6 };
 
            double mean = number.Mean();
            double variance = number.Variance();
            double sd = number.StandardDeviation();
 
            Console.WriteLine("Mean: {0}  , Variance: {1}  , SD: {2}  ", 
                               mean, variance, sd);
            Console.ReadKey();
        }
    }
    public static class list
    {
        public static double Mean(this List<double> values)
        {
            return values.Count == 0 ? 0 : values.Mean(0, values.Count);
        }
 
        public static double Mean(this List<double> values, 
                                  int start, int end)
        {
            double s = 0;
 
            for (int i = start; i < end; i++)
            {
                s += values[i];
            }
 
            return s / (end - start);
        }
 
        public static double Variance(this List<double> values)
        {
            return values.Variance(values.Mean(), 0, values.Count);
        }
 
        public static double Variance(this List<double> values, double mean)
        {
            return values.Variance(mean, 0, values.Count);
        }
 
        public static double Variance(this List<double> values, double mean, 
                                      int start, int end)
        {
            double variance = 0;
 
            for (int i = start; i < end; i++)
            {
                variance += Math.Pow((values[i] - mean), 2);
            }
 
            int n = end - start;
            if (start > 0) n -= 1;
 
            return variance / (n);
        }
        public static double StandardDeviation(this List<double> values)
        {
            return values.Count == 0 ? 0 : values.StandardDeviation(0, values.Count);
        }
        public static double StandardDeviation(this List<double> values, 
                                               int start, int end)
        {
            double mean = values.Mean(start, end);
            double variance = values.Variance(mean, start, end);
            return Math.Sqrt(variance);
        }
    }
}
Program Explanation

This C# program is used to find the standard deviation of a set of given numbers. Create a list using ‘number’ variable. For loop is used to compute the mean from the start and up to the end of the list.

advertisement
advertisement

Compute the summation of all the values and assigning the value to‘s’ variable. Divide the value of ‘s’ variable value by the difference of end and start variable value. For loop is used to compute the mean from the start and up to the end of the list. Compute the power value by 2 to the difference between the values in the list by the mean variable value.

If condition statement is used to check the value of ‘start’ variable is greater than 0. If the condition is true then execute the statement and assign negative value of one to the n variable. Divide the value of ‘variance’ variable by the value of ‘n’ variable.

Compute the square to the value of ‘variance’ variable. Print the computed values of the mean, variance and standard deviation.

Note: Join free Sanfoundry classes at Telegram or Youtube
Runtime Test Cases
 
Mean : 3.5  Variance : 2.916666666667  S.D = 1.7078251256993

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

advertisement

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.