Java program for finding average of array

 Program - 

import java.util.Scanner;

public class Main 

{

    public static void main(String[] args)

    {

        int sum=0 , avg;

        Scanner sc = new Scanner(System.in);

        System.out.println("enter a size of array");

        int n=sc.nextInt();

        int a[] = new int[n];

        System.out.println("enter a element you want in array");

        for (int i=0 ; i<n ;i++)

        {

            a[i]= sc.nextInt();

            

        }

        System.out.println("Your element are");

        for (int i=0 ; i<n ; i++)

        {

            System.out.println(a[i]);

            sum=sum+a[i];

        }

        avg=sum/n;

        System.out.println("Your average for a number is "+avg);

        

    }

}


Output - 

enter a size of array

3

enter a element you want in array

1

2

3

Your element are

1

2

3

Your average for a number is 2



Comments

All time Best Post

Write a program that declares a class named Person. It should have instance variables to record name, age and salary. Use new operator to create a Person object. Set and display its instance variables.

express session login system using node express mongodb

IMAGE CRUD OPERATION USING EXPRESS MONGOOSE MULTER

connect websocket to binance api and store t he true value in the db

CRUD APP WITH SORT IN NODE EXPRESS AND MONGODB

Rock-Paper-Scissor Game in Pure JavaScript

how to delete image from the folder directly from a Request (MULTER)

calculate rsi

java program for 1 to 10 print using while loop