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 program in java:-

To get the average of the numbers you have to build the sum of them and then divide through the number of arguments.

import java.util.*;
class Average
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);// to take user input
int choice;
int a=0,min=0,max=0,x;
int n =args.length;
System.out.println("1-sum");
System.out.println("2-Average");
System.out.println("3-Minimum");
System.out.println("4-Maximum");
System.out.println("Enter Your Choice : ");
choice=sc.nextInt();
for(int i=0;i<n;i++){
a+=Integer.parseInt(args[i]);//to convert string into Integer
}
switch(choice)
{
case 1 :System.out.println("The sum is : "+a);
break;
case 2 :System.out.println("The Average is : "+a/n);
break;
case 3 :for(int i=0;i<n-1;i++)
{ x=Integer.parseInt(args[i]);
if(x<Integer.parseInt(args[i+1]))
min=x;
else min=Integer.parseInt(args[i+1]);
}
System.out.println("The minimum is : "+min);
break;
case 4 :
for(int i=0;i<n-1;i++)
{ x=Integer.parseInt(args[i]);
if(x>Integer.parseInt(args[i+1]))
max=x;
else
max=Integer.parseInt(args[i+1]);
}
System.out.println("The maximum is : "+max);
break;
}
}
}

Related Posts