Tested Java Programs

Just another WordPress.com weblog

Program to find sum,average,minimum and maximum of N numbers using command line arguments

/* Program to find sum,average,minimum and maximum of N numbers using command line arguments */

import java.io.*;
class prog_1a
{
public static void main(String args[])
{
int sum=0,i,count=0,max,min,n;
float avg;
if (args.length == 0)
System.out.println(“Argument is expected “);
else
{
i = args.length;
max = Integer.parseInt(args[0]);
min = Integer.parseInt(args[0]);
while(count < i)
{
n = Integer.parseInt(args[count]);
sum += n;
if(min > n)
min = n;
if (max < n)
max = n;
count += 1;
}
avg = sum/i;
System.out.println(“The sum is ” +sum);
System.out.println(“The average is ” +avg);
System.out.println(“Maximum is ” +max);
System.out.println(“Minimum is ” +min);
}
}
}

August 4, 2009 - Posted by | Uncategorized

No comments yet.

Leave a comment