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.
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.
#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
#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
Login/Signup to comment