✓ Link copied to clipboard
C 📅 July 8, 2026 ⏱ 5 min read

C Program to Calculate Standard Deviation — Full Code & Output

C Program to Calculate Standard Deviation — Full Code & Output

Calculating standard deviation is common in stats. It helps figure out how your data's spread out. If you've got numbers like 2, 4, 4, 4, and 5, you’ll wanna know how much they deviate from the average. This C program takes a list and calculates their standard deviation.

Standard deviation might seem tricky, but it’s super useful. Let’s dive into coding it in C, and you'll get the hang of its application.

Prerequisites for the C Standard Deviation Program

Before diving into the code, ensure you have the following:

  • Understanding of arrays in C.
  • Familiarity with loops and arithmetic operations.
  • Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler.

Source Code of C Program to Calculate Standard Deviation

Here's the complete C program to calculate the standard deviation for a set of numbers. Follow along with the comments to understand each part.

standard_deviation.cc▶ Run
#include <stdio.h>
#include <math.h>

int main() {
    int n, i;
    float sum = 0, sumSq = 0, mean, variance, std_deviation;
    int element[100];

    // Read number of elements
    printf("Enter the number of elements: ");
    scanf("%d", &n);

    // Read elements
    printf("Enter %d elements: ", n);
    for (i = 0; i < n; i++) {
        scanf("%d", &element[i]);
        sum += element[i];
        sumSq += element[i] * element[i];
    }

    // Calculate mean and variance
    mean = sum / n;
    variance = (sumSq / n) - mean * mean;

    // Calculate standard deviation
    std_deviation = sqrt(variance);

    printf("Standard Deviation = %.3f", std_deviation);

    return 0;
}

Try this code yourself — free

Run it in the EduSeekho online compiler. No setup needed.

▶ Open Compiler

Output of the Standard Deviation Program

Here's what you can expect when you run the program.

Output
Enter the number of elements: 5
Enter 5 elements: 2 4 4 4 5
Standard Deviation = 1.095
Output of C Program to Calculate Standard Deviation
Output of C Program to Calculate Standard Deviation

Algorithm to Calculate Standard Deviation in C

Standard deviation tells how much data deviates from the average. This program reads numbers, computes their mean, and calculates the variance, then derives the standard deviation.

  1. Read the integer n from the user.
  2. Initialize sum and sumSq to zero.
  3. For each element, accumulate the sum and sum of squares.
  4. Calculate the mean by dividing sum by n.
  5. Compute variance using the accumulated sums.
  6. Calculate standard deviation as the square root of variance.
flowchart TD
  A([Start]) --> B[/Read n/]
  B --> C[sum = 0, sumSq = 0]
  C --> D{for each element}
  D -->|Yes| E[sum += element[i]]
  E --> F[sumSq += element[i] * element[i]]
  F --> D
  D -->|No| G[mean = sum / n]
  G --> H[variance = (sumSq / n) - mean * mean]
  H --> I[std_deviation = sqrt(variance)]
  I --> J([End])
Flowchart: the algorithm step by step

How the C Program to Calculate Standard Deviation Works

Let's break down how this program calculates standard deviation step by step:

  • #include <stdio.h> and #include <math.h> bring in necessary functions for input/output and math operations.
  • The program starts by declaring variables like n, sum, sumSq, mean, and storage for elements.
  • It reads the number of elements n and then proceeds to read each element into the array element.
  • The loop sums up the elements and their squares, updating sum and sumSq.
  • Mean is calculated by dividing sum by n, giving the average of numbers.
  • The variance is then calculated using (sumSq / n) - mean * mean.
  • Finally, sqrt computes the standard deviation from the variance, which is printed out.
🧠

Quick Quiz — test your understanding

Tap an option to check your answer

What does the standard deviation measure?

🎉Correct! Standard deviation measures the spread of data values.
🤔Not quite. Think about what indicates how data values vary.

Time and Space Complexity of the C Standard Deviation Program

Considering performance, here's how the program fares:

Time Complexity: O(n). n is the number of elements entered by the user—e.g., 5 for five numbers. We loop over all numbers once to calculate the sum and sum of squares. Each additional element requires one more loop cycle.

Space Complexity: O(n). We store the array element of size n, but also use a few additional variables like sum, sumSq, and mean. These variables do not grow with the input size.

⚠️

Common mistake:Not including #include <math.h> can cause undefined behavior when using sqrt.

Key takeaway:Standard deviation shows how spread out the values in a data set are, and it’s a great tool in data analysis.

FAQs About Calculating Standard Deviation in C

Here are some common questions students have about this program.

Standard deviation measures variation or dispersion in values.
The <math.h> library's needed for functions like sqrt to calculate square roots.
Negative numbers are treated like positives for standard deviation. Just make sure the variance's calculated right to avoid a negative square root error.
Yeah, but ensure the array size can hold all data entries. Adjust the element array size if needed.
We use sum for the mean and sumSq for the variance, which involves squaring each number.
Change the data type of element to float and tweak the scanf format specifier accordingly.

If you found this program helpful, check out these related programs:

Conclusion

Standard deviation’s an important concept in statistics and data analysis. This C program shows you how it's calculated and why it matters. Try playing around with different data sets to get a better feel for its uses.

Test your knowledge with our C programming quiz and run similar programs in our online compiler.