In this tutorial you will learn about the C Program to Calculate Standard Deviation and its application with practical example.
C Program to Calculate Standard Deviation
In this tutorial, we will learn to create a C program that will Calculate Standard Deviation in C programming.
Prerequisites
Before starting with this tutorial, we assume that you are the best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- Conditional Statements in c programming.
- User-defined functions in C programming.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 |
1. Declare the variables for the program. 2. Taking the input numbers five elements. 3. Passing that array to the user defined loop. 4. Calculating the standard deviations. 5. Print the standard deviations. 6. End the program. |
Program to Calculate Standard Deviation.
In this program, we will first take the five elements from the user. After taking the input, we will add all the elements. Then we will find its standard deviation, using the mathematical expression, and then we will print the standard deviation of five elements.
With the help of this program, we can Calculate Standard Deviation.
Program Code:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
//C Program to Calculate Standard Deviation //Including the required header files for the program. #include <math.h> #include <stdio.h> /* Declaring the user defined function to calculate the standard deviation */ float calculateSD(float arry[]); //Main function. int main() { //Declaring the required variable for the program. int i; float arry[5]; //i = it will hold integer value for the loop. //arry[5] = it will hold integer value for the input number. //taking input numbers for the program. printf("Enter 5 elements: "); for (i = 0; i < 5; ++i) scanf("%f", &arry[i]); //printing the standard deviation. printf("\nStandard Deviation = %.6f", calculateSD(arry)); return 0; } //Calculating the standard deviation float calculateSD(float arry[]) { float sum = 0.0, mean, SD = 0.0; int i; //performing the calculations for the standard deviation for (i = 0; i < 5; ++i) { sum += arry[i]; } mean = sum / 5; for (i = 0; i < 5; ++i) { SD += pow(arry[i] - mean, 2); } return sqrt(SD / 5); } |
Output:-
In the above program, we have first initialized the required variable.
- i = it will hold the integer value of the loop.
- arry[5] = it will hold the integer value.
Taking the input elements of the array.
In this section of the code of the program, we will calculate the SD of the numbers.
Printing the standard deviation of the numbers.