Java program to find average of numbers in the array

import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(; System.out.println("Enter +ve array size"); int n=sc.nextInt(); double arr1[]=new double[n]; double result1= 0.0; //using for loop double result2= 0.0; //using for each loop System.out.println("Enter array elements"); for(int i=0;i<n;i++) { arr1[i]=sc.nextDouble(); } for(int i=0;i<n;i++) { result1 = result1 +arr1[i]; //result1 += arr[i]; } System.out.println("Using For loop Average is "+ result1/n); for (double arr: arr1) { result2 += arr;//result2 = result2 + arr; } System.out.println("Using For each loop Average is "+ result2/n); } }

Similar Program