Finding average and sum of a given array.


Write a program to find sum of array and average number of array element in java.

Write java code to find average and sum of array.


import java.io.*;

public class find_avgandsumofarray {

            public static void main(String[]args)throws IOException

            {

                        int size,i;

                        float avg=0,sum=0;

                        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

                        System.out.print("enter the size of array:- ");

                        size=Integer.parseInt(br.readLine());

                        int a[]=new int[size];

                        System.out.print("enter the elements of array:");

                        for(i=0;i<a.length;i++)

                        {

                                    a[i]=Integer.parseInt(br.readLine());

                                    sum=sum+a[i];

                        }

                        avg=(sum/size);

                        System.out.println("sum of array elementis:"+sum+"\naverage is aray element:"+avg);

                       

            }

 

}

No comments:

Post a Comment