Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to find average of N numbers through a Java program ?.

A simple Java program to find average of N numbers





package com.hubberspot.sample.example;

import java.util.Scanner;

public class Average {
  public static void main(String[] args) {
    
    Scanner input = new Scanner(System.in);  
    System.out.print("Enter N number of elements " +
   "for finding average : "); 
 
    final int elements = input.nextInt();;
    System.out.println(""); 
 
    double[] numbers = new double[elements];
    double sum = 0;

    
    for (int i = 0; i < elements; i++) {
      System.out.print("Enter number "+(i+1)+" : " );
      numbers[i] = input.nextDouble();
      sum += numbers[i];
    }
    
    double average = sum / elements;
    
    System.out.println(""); 
    System.out.println("Average is " + average);
    
  }
}
Output of the program : 


 
 
© 2021 Learn Java by Examples Template by Hubberspot