Saturday, 6 June 2020

Program to read values from command line and find max in java


Program to read values from command line argument and find maximum in java.

In the below java program  we read some values from command line argument and the maximum among them.
class Demomaximum {   
   public static void main (String [] args)       {    
       int max=0;
                  
                   int a[]=new int [args.length];
                   for(int i=0;   i < a.length;   i++){
                         a[i] = Integer.parseInt(args[i]);
                   }
                  
                        //  finding maximum value
                   max=a[0];
                   for(int i=0  ;  i  <a.length; i++){
                                                if(a[i]>max){
                                                    max=a[i];
                                                }
                   }
                   System.out.println("max = "+max);
                }
                }

compile:
C:\>  javac Demomaximum.java
Run:
C:\>  java    Demomaximum    10  15   5   25   15   40   12  30
  Max= 40

No comments:

Post a Comment