sum of array
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
///////////////////
// FifthJava.java
//
// javac FifthJava.java
//
// produces FifthJava.class
//
// To execute
// ----------
// java FifthJava < list of arguments>
//
//
//
//
public class FifthJava
{
public static void main(String[] args)
{
//
// retreive the number of arguments
int n = args.length;
System.out.println(n);
//
// declare a double array (vector)
double [] arr = new double[n];
//
// converting strings into double array
for(int i = 0;i < n;i++) {
arr[i] = Double.parseDouble(args[i]);
}
//
// computing the sum of the contents of double array
double sum = 0;
int k = 0;
while (k < n) {
sum = sum + arr[k];
k = k+1;
}
//
// printing the sum
System.out.println(sum);
}
}
No comments:
Post a Comment