Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Add all elements in an array

$
0
0
Hi, I need help on this Sum problem. I want to add all the array elements into one value which is '19.0', instead it prints out:

1.0
3.0
7.0
12.0
18.0

as I want to use the sum output later, I only want it to have 1 value, instead it has many.

any help?


public class hi {

    public static void main(String[] args) {

        double[] ra = {1.0, 2.0, 4.0, 5.0, 6.0};
        Sum(ra);
}

    public static void Sum(double[] a){

        double sum = 0;

        for (int i = 0; i < a.length; i++){
            sum+= a[i];
            System.out.println(sum);

        }
    }

}


one value which is '19.0' = '18.0'

sorry! Typo

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>