Modify the code to print only the average of all the integer

Modify the code to print only the average of all the integers from the ArrayList.

Loopy2.java

import java.util.ArrayList;
import java.util.Scanner;


public class Loopy2 {
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       ArrayList<Integer> list = new ArrayList<Integer>();
       System.out.println(\"Enter an arbitrary number of integers: \");
       for(;;){
           String s = scan.next();
           if(s.equalsIgnoreCase(\"Q\")){
               break;
           }
           else{
               list.add(Integer.valueOf(s));
           }
       }
       System.out.println(\"ArrayList values are: \");
       for(int i=0;i < list.size(); i++){
           System.out.println(list.get(i));
       }
   }
}

Solution

import java.util.Scanner; public class Sum_Average { public static void main(String[] args) { int n, sum = 0; float average; Scanner s = new Scanner(System.in); System.out.print(\"Enter no. of elements you want in array:\"); n = s.nextInt(); int a[] = new int[n]; System.out.println(\"Enter all the elements:\"); for(int i = 0; i < n ; i++) { a[i] = s.nextInt(); sum = sum + a[i]; } System.out.println(\"Sum:\"+sum); average = (float)sum / n; System.out.println(\"Average:\"+average); } }
Modify the code to print only the average of all the integers from the ArrayList. Loopy2.java import java.util.ArrayList; import java.util.Scanner; public class

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site