Java sum & average

By | 12/11/2014

This program calculates the sum and average of given numbers.

SumAndAverage.java:

import java.util.Scanner;

public class SumAndAverage {
 public void run()
 {
  Scanner reader = new Scanner(System.in);
		
  int sum=0, newNumber=0, amount=0;
  double average;
		
  System.out.print("Enter numbers. They will be summed together,\nuntil you enter the numbers between 200 and 209. ");
		
  do
  {
   amount++;
   newNumber=reader.nextInt();
   sum=sum+newNumber;
  } while(newNumber<200 || newNumber>209);
		
  average=(sum/amount)*1.0;
  System.out.println("The sum of the given numbers is: "+sum+". \nTheir average is: "+average);
  reader.close();
 }
 public static void main(String[] args)
 {
  SumAndAverage program = new SumAndAverage();
  program.run();
 }
}

Leave a Reply

Your email address will not be published. Required fields are marked *