4-1 Sum_average

posted Feb 2, 2014, 10:14 AM by Dlovan ismail   [ updated Feb 2, 2014, 10:14 AM by Karwan Jacksi ]
//Write a java program that reads N integer numbers from the keyboard then calculate sum and average of them.

package sum_average;
import java.util.Scanner;//Needed for class Scanner

public class Sum_average 
{
    public static void main(String[] args) 
    {
            //---Create a class Scanner for inputting data.---
            Scanner Keyboard=new Scanner(System.in);

            //---Print Enter the value of n : on the screen.---
            System.out.print("Enter the value of n : ");

            /*---N : represent numbers of inputting data from keyboard.---
              ---initial : take the initial value 1.---
              ---sum : represent sum of all numbers you entered from keyboard.---*/

            int N=Keyboard.nextInt(),initial=1,sum=0;

            while(initial<=N)
            {
                System.out.print("Enter number"+initial+" : ");
                sum+=Keyboard.nextInt();
                initial++;
            }

            double average=sum/(double)N;
            System.out.printf("\nSum=%d\nAverage=%f\n",sum,average);
    }   
}

ċ
Sum_average.java
(1k)
Karwan Jacksi,
Feb 2, 2014, 10:14 AM
Comments