Array: Sum Integer from command line array input

Question: Write a program that passes an unspecified number of integers from the command line and displays their total.

Sample input/output:

args: [“1”, “2”, “3”, “4”, “5”]
Sum of numbers: 15

package Ch7;

/*
    Q: Write a program that passes an unspecified number of integers from command line and displays their total.
 */

public class Ex21 {

    public static void main (String [] args){
        int sum=0;

        for(int i=0; i< args.length; i++){
            sum+= Integer.parseInt(args[i]);
        }

        System.out.println("Sum of numbers: " +sum);
    }
}

/*
    Sample input/output
    args: ["1", "2", "3", "4", "5"]
    Sum of numbers: 15
 */

Leave a Reply

PHP JS HTML CSS BASH PYTHON CODE

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: