You are on page 1of 2

Ex No.

11
Date:
Mean and Variance

Aim:
To write a java program to calculate the mean, variance and standard dev
iation.
Source Code:
import java.io.*;
class Meanvar
{
public static void main(String rk[ ]) throws IOException
{
DataInputStream in= new DataInputStream(System.in);
System.out.println("Enter the no of want to give as input : ");
short n=Short.parseShort(in.readLine());
float a[ ]=new float[n];
float sum=0;
for(short i=0;i<n;i++)
{
System.out.println("Enter element "+(i+1)+" : ");
a[i]=Float.parseFloat(in.readLine());
sum=sum+a[i];
}
float mean=sum/n;
float sumsqr=0;
for(short i=0;i<n;i++)
{
sumsqr=sumsqr+((a[i]-mean)*(a[i]-mean));
}
float variance=sumsqr/n;
double sd=Math.sqrt(variance);
System.out.println("mean : " +mean);
System.out.println("Variance :"+variance);
System.out.println("SD : "+sd);
}
}
Output:
javac Meanvar.java
java Meanvar
Enter the no of want to give as input :
4
enter element1:
1
enter element2:
2
enter element3:
3
enter element4:
4
Mean :2.5
Variance :1.25
sd :1.118033988749895
Resullt:
Thus a java program has been written to calculate the mean, variance and
standard deviation.