#! /usr/bin/perl

# sum using while loop

# add up elements of array
sub sum2 {
    my $i = 0; my $s = 0;
    while ($i < @_) {
        # $s is the sum of the first $i array elements
        # $s == $_[$0] + .. + $_[$i-1]
	$s = $s + $_[$i];
	$i = $i + 1;
    }
    return $s;
}

# main program

@a1 = (2, 4, 6, 8);
print "The sum of elements in @a1 is ", sum2(@a1), "\n";



