Loading

Programs to find an average of N numbers using the iterative method:                                 

package quipoin.javaeasyprograms;
public class AverageOfNNumber {

	static double average(double... n) {
		double sum = 0;
		int count = 0;
		for (double x : n) {
			sum += x;
			count++;
		}
		if (count == 0)
			return 0;
		else
			return sum / count;
	}

	public static void main(String[] args) {
		System.out.println(average(1, 2));
		System.out.println(average(1, 2, 3));
	}
}

Output:

1.5
2.0