Q-01: Write a JAVA program to find the sum of any number of integers entered as command line arguments.
    

Approach-1


// Contributed by - Anuj Das ( GC College, Silchar - @ Department of Computer Science ) class CommandLineArgs { public static void main(String[ ] args) { int []x = new int[100]; int sum=0; for(int i=0; i < args.length; i++) { x[i] = Integer.parseInt(args[i]); sum += x[i]; } System.out.println("The Sum of Integers: " + sum); } }

OUTPUT

cmd->  javac  CommandLineArgs.java
cmd->  java  CommandLineArgs  51  19  65  78  12

The Sum of Integers: 225


  

Approach-2


// Contributed by - Anuj Das ( GC College, Silchar - @ Department of Computer Science ) class CLArgs { public static void main(String ...args) { int sum=0; for(String number : args) { sum += Integer.parseInt(number); } System.out.println("The Sum of Integers: " + sum); } }

OUTPUT

cmd->  javac  CLArgs.java
cmd->  java  CLArgs  45 75 12

The Sum of Integers: 132