Algorithm


  1. Input: Array of numbers (data)
    2. Calculate the mean (average) of the data.
       - sum = 0
       - for each number in data:
           - sum = sum + number
       - mean = sum / number of elements in data

    3. Calculate the squared difference between each number and the mean.
       - for each number in data:
           - difference = number - mean
           - squared_difference = difference * difference

    4. Calculate the mean of the squared differences.
       - sum_squared_differences = 0
       - for each squared_difference:
           - sum_squared_differences = sum_squared_differences + squared_difference
       - mean_squared_differences = sum_squared_differences / number of elements in data

    5. Calculate the square root of the mean_squared_differences.
       - standard_deviation = square root of mean_squared_differences

    6. Output: standard_deviation

Important Note :

 - The standard deviation can be calculated as the square root of variance by determining the deviation of each data         point relative to the mean.

 - Standard deviation is a measure which can show how much variance are exist.

Code Examples

#1 Code Example- C Programing to calculate the sum of n natural number using for loop

Code - C Programming

#include<stdio.h>
#include<math.h>
int main ()
{
  float Price[50];
  int i, Number;
  float Mean, Variance, SD, Sum = 0, Differ, Varsum = 0;
  printf ("Please Enter the N Value = ");
  scanf ("%d", &Number);
  printf ("\nPlease Enter %d real numbers\n", Number);
  for (i = 0; i < Number; i++)
    {
      scanf ("%f", &Price[i]);
    }
  for (i = 0; i < Number; i++)
    {
      Sum = Sum + Price[i];
    }
  Mean = Sum / (float) Number;
  for (i = 0; i < Number; i++)
    {
      Differ = Price[i] - Mean;
      Varsum = Varsum + pow (Differ, 2);
    }
  Variance = Varsum / (float) Number;
  SD = sqrt (Variance);
  printf ("Standard deviation = %.2f\n", SD);
Copy The Code & Try With Live Editor

Output

x
+
cmd
Please Enter the N Value = 5
Please Enter 5 real numbers
11
22
33
44
55
Standard deviation = 20.25

#2 Code Example-C programing to calculate the standard deviation

Code - C Programming

#include<math.h>
#include<stdio.h>
float calculateSD (float data[]);
int main ()
{
  int i;
  float data[10];
  printf ("Enter 10 elements: ");
  for (i = 0; i < 10; ++i)
    scanf ("%f", &data[i]);
  printf ("\nStandard Deviation = %.6f", calculateSD (data));
  return 0;
}
float calculateSD (float data[])
{
  float sum = 0.0, mean, SD = 0.0;
  int i;
  for (i = 0; i < 10; ++i)
    {
      sum += data[i];
    }
  mean = sum / 10;
  for (i = 0; i < 10; ++i)
    {
      SD += pow (data[i] - mean, 2);
    }
  return sqrt (SD / 10);
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Enter 10 elements: 1
2
33
44
55
66
77
88
99
110
Standard Deviation = 39.271492

#3 Code Example- C programing to calculate the sum of n natural number using do-while loop

Code - C Programming

#include<stdio.h>
int main ()
{
  int n = 4, i, sum = 0;
  i = 1;
  do
    {
      sum += i;
      ++i;
    }
  while (i <= n);
  printf ("Sum of natural numbers = %d", sum);
  return 0;
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Sum of natural numbers = 10
Advertisements

Demonstration


C Programing Example for Standard Deviation-DevsEnv

Next
Appending into a File in C Programming