Program for Standard Deviation

Standard Deviation

We will write a C program for standard deviation and the number will be entered by the user. This will help to understand the basic structure of programming. In this program , we will display the standard deviation of given input easily by using proper syntax and algorithms.

Program for Standard Deviation

Working of Program :

In the program, we will require some numbers from the user to display the standard deviation of given input.

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.

Syntax of for() Loop:-

for (Initialization, condition, updation)
{
    //code
}

Problem 1

Write a program to calculate the sum of n natural number using for loop.

  • Firstly, we have to enter the number.
  • Then print the sum of natural numbers.

Code

Run
#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);

Output

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

Note:

In the following program we will display the standard deviation for given input.

Problem 2

Write a program to calculate the standard deviation.

  • Firstly, we have to enter the number.
  • Then print the standard deviation.

Code

Run
#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);
}

Output

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

Syntax for do-while() Loop:-

do
{
    // Statements
    // Increment / Decrement
}
while(condition)

Problem 3

Write a program to calculate the sum of n natural number using do-while loop.

  • Firstly, we have to enter the number.
  • Then print the sum of natural numbers.

Code

Run
#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;
}

Output

Sum of natural numbers = 10

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription