Write a java program that displays the sum of the commandlin

Write a java program that displays the sum of the command-line arguments. For example, if you enter

java C9h1 1 3.5 7

your C9h1 program will display

Sum = 11.5

Your java program should work with any number of command-line arguments. (Hint: Use Double.parseDouble, which converts a string to type double)

Solution

The java program that displays the sum of the command-line arguments :-

class Addition

{

public static void main(String[ ] args)

{

int x = Integer.parseInt(args[0]) ; //Integer.parseInt converts a string to type int

int y = Integer.parseInt(args[1]) ; //Integer.parseInt converts a string to type int

int z = x+y ;

System.out.println(\"Sum : =\" + z) ;

}

}

Note :- we must execute above program by passing at least two int values.

Compilation :- javac Addition.java

Execution :- java Addition 5 6

Sum : = 11

Write a java program that displays the sum of the command-line arguments. For example, if you enter java C9h1 1 3.5 7 your C9h1 program will display Sum = 11.5

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site