b

b

Tuesday, February 23, 2016

Foo.java Averaging Numbers

 Foo.java for Total and Average of numbers. To run this program go to 'run configuration' Click on 'Arguments' tab... Write any numbers with space between them to get and average of that numbers.

//**********************************************************************
//  Averaging Numbers   Foo.java
//**********************************************************************

public class Foo
{
    public static void main(String args[])
    {
        int sum=0,i,count=0,n;
        float avg;
        if (args.length == 0)
            System.out.println("No arguments");
        else
        {
            i = args.length;
            System.out.println("Number of arguments is " + i);
           
            while(count < i)
            {
                n = Integer.parseInt(args[count]);
                sum += n;
                count += 1;
            }
            avg = sum/i;
            System.out.println("The sum is " +sum);
            System.out.println("The average is " +avg);

        }
    }
}

No comments:

Post a Comment