Program to calculate mean value of an array | Java Programs

 

Code:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        int n;

        System.out.print("Enter size of array: ");
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        int a[] = new int[n];

        for (int i = 0; i < a.length; i++) {
            System.out.print("Enter " + (i + 1) + " element: ");
            a[i] = sc.nextInt();
        }

        int sum = 0;
        for (int j = 0; j < a.length; j++) {
            sum = sum + a[j];
        }

        int mean;
        mean = sum / a.length;
        System.out.println("\nMean = " + mean);
    }
}


Output:

AJ Blogs

Hello everyone, My name Arth and I like to write about what I learn. Follow My Website - https://sites.google.com/view/aj-blogs/home

Post a Comment

Previous Post Next Post
Best Programming Books

Facebook

AJ Facebook
Checkout Our Facebook Page
AJ Blogs
Checkout Our Instagram Page