Averages/Root mean square: Difference between revisions

Content added Content deleted
(Added ZX81 BASIC)
(→‎{{header|Java}}: consistent formatting, cleanup)
Line 785: Line 785:


=={{header|Java}}==
=={{header|Java}}==
<lang java>public class RMS {
<lang java>public class RootMeanSquare {

public static double rms(double[] nums){
double ms = 0;
public static double rootMeanSquare(double... nums) {
for (int i = 0; i < nums.length; i++)
double sum = 0.0;
ms += nums[i] * nums[i];
for (double num : nums)
ms /= nums.length;
sum += num * num;
return Math.sqrt(ms);
return Math.sqrt(sum / nums.length);
}
}


public static void main(String[] args){
public static void main(String[] args) {
double[] nums = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0};
double[] nums = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0};
System.out.println("The RMS of the numbers from 1 to 10 is " + rms(nums));
System.out.println("The RMS of the numbers from 1 to 10 is " + rootMeanSquare(nums));
}
}
}</lang>
}</lang>