The variance is the mean of the square, minus the square of the mean.

So if you have a data set (in this case, X), then you simply square each number, and find the mean (sum and divide by how many there are), then you subtract from this the mean of the set squared.

public double Var(int [] X)

{

double E_X2=0.0;

double EX_2=0.0;

for(int i=0; i<X.length; i++)

{

E_X2 += X[i]*X[i];

EX_2 += X[i];

}

E_X2 /= X.length;

EX_2 /= X.length;

return E_X2 - EX_2;

}

So if you have a data set (in this case, X), then you simply square each number, and find the mean (sum and divide by how many there are), then you subtract from this the mean of the set squared.

public double Var(int [] X)

{

double E_X2=0.0;

double EX_2=0.0;

for(int i=0; i<X.length; i++)

{

E_X2 += X[i]*X[i];

EX_2 += X[i];

}

E_X2 /= X.length;

EX_2 /= X.length;

return E_X2 - EX_2;

}