Pin It

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 : 


 
Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot