import java.util.Scanner;
public class apples {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
System.out.println("How many values you want to use: ");
//get number
int n = input.nextInt();
//create array with size n
int[] array = new int[n];
//get the input numbers using for loop
for (int m = 0; m < n; m++) {
System.out.print("Value nr " + m + " : ");
array[m] = input.nextInt();
}
System.out.println("Mean is: " + getMean(array));
}
public static double getMean(int[] n){
double sum = 0;
for (int m = 0; m < n.length; m++) {
sum+=n[m];
}
return ((double)sum) / n.length;
}
}
Friday, 14 December 2012
Mean of n numbers using Scanner class
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment