Java Programming : Write a JAVA Program to find the Average and Sum of an Array.

Posted on:
tags: ,


     Write a JAVA Program to find the Average and Sum of an Array.
import java.io.*;
import java.util.*;
public class a16 {
   

     public static void main(String args[])throws IOException
                 {
                        Scanner br = new Scanner (System.in);
                        System.out.println("Enter the no. of elements");
                        int n = br.nextInt();

                             int a[]=new int[n];
                        int sum=0,i,avg;
                             System.out.println("Enter the elements");
                        for(i=0;i<n;i++)
                a[i] = br.nextInt();
                         for(i=0;i<n;i++)
                        {
                        sum=sum+a[i];
                        }
                        System.out.println("the sum is"+sum);
                        avg=sum/n;
                        System.out.println("the avg is"+avg);
                        }
                        }
OUTPUT:
Enter the no. of elements
4
Enter the elements
4
5
6
7
the sum is22
the avg is5

No comments:

Post a Comment

< >