# Program to implement standard deviation of grouped data

Last Updated : 07 Oct, 2022

Given a class interval and frequency of the class and the task is to find standard deviation of grouped data.
Formula to find standard deviation

Standard Deviation = ?( (?(F x M2 – n x ?2)) / (n-1) )
Where,
F – frequency of the class.
M – mid value of class interval.
? – Mean of the grouped data.
n – sum of frequency.

Examples:

```Input : lower_limit[] = {50, 61, 71, 86, 96}
upper_limit[] = {60, 70, 85, 95, 100}
freq[] = {9, 7, 9, 12, 8}
Output : 15.757
Explanation :
Standard deviation = sqrt(287127.75 - 45 *
78.3444 * 78.3444) / (45 - 1)
= 15.757

Input : lower_limit[] = {37, 47, 57, 67}
upper_limit[] = {46, 56, 66, 76}
freq[] = {19, 23, 27, 28}
Output : 10.9817```

## CPP

 `// CPP Program to implement standard ` `// deviation of grouped data.` `#include ` `using` `namespace` `std;`   `// Function to find mean of grouped data.` `float` `mean(``float` `mid[], ``int` `freq[], ``int` `n)` `{` `    ``float` `sum = 0, freqSum = 0;` `    ``for` `(``int` `i = 0; i < n; i++) {` `        ``sum = sum + mid[i] * freq[i];` `        ``freqSum = freqSum + freq[i];` `    ``}` `    ``return` `sum / freqSum;` `}`   `// Function to find standard` `// deviation of grouped data.` `float` `groupedSD(``float` `lower_limit[],` `                ``float` `upper_limit[],` `                ``int` `freq[], ``int` `n)` `{` `    ``float` `mid[n], sum = 0, freqSum = 0, sd;` `    ``for` `(``int` `i = 0; i < n; i++) {` `        ``mid[i] = (lower_limit[i] + upper_limit[i]) / 2;` `        ``sum = sum + freq[i] * mid[i] * mid[i];` `        ``freqSum = freqSum + freq[i];` `    ``}`   `    ``// Formula to find standard deviation` `    ``// of grouped data.` `    ``sd = ``sqrt``((sum - freqSum * mean(mid, freq, n) * ` `              ``mean(mid, freq, n)) / (freqSum - 1));` `    ``return` `sd;` `}`   `// Driver function.` `int` `main()` `{` `    ``// Declare and initialize` `    ``// the lower limit of interval.` `    ``float` `lower_limit[] = { 50, 61, 71, 86, 96 };`   `    ``// Declare and initialize` `    ``// the upper limit of interval.` `    ``float` `upper_limit[] = { 60, 70, 85, 95, 100 };` `    ``int` `freq[] = { 9, 7, 9, 12, 8 };`   `    ``// Calculating the size of array.` `    ``int` `n = ``sizeof``(lower_limit) / ``sizeof``(lower_limit[0]);`   `    ``cout << groupedSD(lower_limit, upper_limit, freq, n);` `    ``return` `0;` `}`

## Java

 `// Java program to implement ` `// standard deviation of grouped data.` `import` `java.io.*;`   `class` `GFG {` `    `  `    ``// Function to find mean of grouped data.` `    ``static` `float` `mean(``float` `mid[], ``int` `freq[], ``int` `n)` `    ``{` `        ``float` `sum = ``0``, freqSum = ``0``;` `        ``for` `(``int` `i = ``0``; i < n; i++) ` `        ``{` `            ``sum = sum + mid[i] * freq[i];` `            ``freqSum = freqSum + freq[i];` `        ``}` `        ``return` `sum / freqSum;` `    ``}` `    `  `    ``// Function to find standard` `    ``// deviation of grouped data.` `    ``static` `float` `groupedSD(``float` `lower_limit[],` `                          ``float` `upper_limit[],` `                            ``int` `freq[], ``int` `n)` `    ``{` `        ``float` `mid[] = ``new` `float``[n];` `        ``float` `sum = ``0``, freqSum = ``0``, sd;` `        ``for` `(``int` `i = ``0``; i < n; i++) ` `        ``{` `            ``mid[i] = (lower_limit[i] + upper_limit[i]) / ``2``;` `            ``sum = sum + freq[i] * mid[i] * mid[i];` `            ``freqSum = freqSum + freq[i];` `        ``}` `    `  `        ``// Formula to find standard deviation` `        ``// deviation of grouped data.` `        ``sd = (``float``)Math.sqrt((sum - freqSum * mean(mid, freq, n) * ` `                        ``mean(mid, freq, n)) / (freqSum - ``1``));` `        ``return` `sd;` `    ``}`   `    ``// Driver function.` `    ``public` `static` `void` `main (String[] args)` `    ``{` `        ``// Declare and initialize` `        ``// the lower limit of interval.` `        ``float` `lower_limit[] = { ``50``, ``61``, ``71``, ``86``, ``96` `};` `    `  `        ``// Declare and initialize` `        ``// the upper limit of interval.` `        ``float` `upper_limit[] = { ``60``, ``70``, ``85``, ``95``, ``100` `};` `        ``int` `freq[] = { ``9``, ``7``, ``9``, ``12``, ``8` `};` `    `  `        ``// Calculating the size of array.` `        ``int` `n = lower_limit.length;` `    `  `        ``System.out.println( groupedSD(lower_limit, ` `                            ``upper_limit, freq, n));` `            `  `    ``}` `}`   `// This code is contributed by vt_m`

## Python3

 `# Python Program to implement standard` `# deviation of grouped data.`   `import` `math`   `# Function to find mean of grouped data.` `def` `mean( mid, freq, n):`   `    ``sum` `=` `0` `    ``freqSum ``=` `0` `    ``for` `i ``in` `range``(``0``,n):` `        ``sum` `=` `sum` `+` `mid[i] ``*` `freq[i]` `        ``freqSum ``=` `freqSum ``+` `freq[i]` `    `  `    ``return` `sum` `/` `freqSum`   ` `  `# Function to find standard` `# deviation of grouped data.` `def` `groupedSD(lower_limit, upper_limit ,freq , n):`   `    ``mid``=``[[``0``] ``for` `i ``in` `range``(``0``,n)]` `    ``sum` `=` `0` `    ``freqSum ``=` `0` `    ``sd``=``0` `    ``for` `i ``in` `range``(``0``,n):` `        ``mid[i] ``=` `(lower_limit[i] ``+` `upper_limit[i]) ``/` `2` `        ``sum` `=` `sum` `+` `freq[i] ``*` `mid[i] ``*` `mid[i]` `        ``freqSum ``=` `freqSum ``+` `freq[i]` ` `  `    ``# Formula to find standard deviation` `    ``# of grouped data.` `    ``sd ``=` `math.sqrt((``sum` `-` `freqSum ``*` `mean(mid, freq, n)``*` `mean(mid, freq, n)) ``/` `(freqSum ``-` `1``))` `    ``return` `sd`     `#  driver code` `# Declare and initialize` `# the lower limit of interval.` `lower_limit``=` `[ ``50``, ``61``, ``71``, ``86``, ``96` `]` ` `  `# Declare and initialize` `# the upper limit of interval.` `upper_limit``=` `[ ``60``, ``70``, ``85``, ``95``, ``100` `]` `freq ``=``[ ``9``, ``7``, ``9``, ``12``, ``8` `]` ` `  `# Calculating the size of array.` `n ``=` `len``(lower_limit)` ` `  `print``(groupedSD(lower_limit, upper_limit, freq, n))`   `# This code is contributed by Gitanjali.`

## C#

 `// C# program to implement ` `// standard deviation of grouped data.` `using` `System;`   `class` `GFG {` `    `  `    ``// Function to find mean of grouped data.` `    ``static` `float` `mean(``float` `[]mid, ``int` `[]freq, ` `                                       ``int` `n)` `    ``{` `        ``float` `sum = 0, freqSum = 0;` `        ``for` `(``int` `i = 0; i < n; i++) ` `        ``{` `            ``sum = sum + mid[i] * freq[i];` `            ``freqSum = freqSum + freq[i];` `        ``}` `        ``return` `sum / freqSum;` `    ``}` `    `  `    ``// Function to find standard` `    ``// deviation of grouped data.` `    ``static` `float` `groupedSD(``float` `[]lower_limit,` `                           ``float` `[]upper_limit,` `                            ``int` `[]freq, ``int` `n)` `    ``{` `        ``float` `[]mid = ``new` `float``[n];` `        ``float` `sum = 0, freqSum = 0, sd;`   `        ``for` `(``int` `i = 0; i < n; i++) ` `        ``{` `            ``mid[i] = (lower_limit[i] + upper_limit[i]) / 2;`   `            ``sum = sum + freq[i] * mid[i] * mid[i];`   `            ``freqSum = freqSum + freq[i];` `        ``}` `    `  `        ``// Formula to find standard deviation` `        ``// deviation of grouped data.` `        ``sd = (``float``)Math.Sqrt((sum - freqSum * mean(mid,` `             ``freq, n) * mean(mid, freq, n)) / (freqSum - 1));` `        `  `        ``return` `sd;` `    ``}`   `    ``// Driver function.` `    ``public` `static` `void` `Main ()` `    ``{` `        ``// Declare and initialize` `        ``// the lower limit of interval.` `        ``float` `[]lower_limit = { 50, 61, 71, 86, 96 };` `    `  `        ``// Declare and initialize` `        ``// the upper limit of interval.` `        ``float` `[]upper_limit = { 60, 70, 85, 95, 100 };` `        ``int` `[]freq = { 9, 7, 9, 12, 8 };` `    `  `        ``// Calculating the size of array.` `        ``int` `n = lower_limit.Length;` `    `  `        ``Console.WriteLine(groupedSD(lower_limit, ` `                         ``upper_limit, freq, n));` `            `  `    ``}` `}`   `// This code is contributed by vt_m`

## PHP

 ``

## Javascript

 ``

Output:

`15.757`

Time Complexity: O(n)
Auxiliary Space: O(n)

Previous
Next