Notice


AWT and SWING will be covered soon !

Monday, September 21, 2009

Get Sum of numbers passing a Array to a Method

 

image

import java.util.Scanner;

public class avg{
    public static void main(String args[])
    {
        Scanner scan = new Scanner(System.in);
        double avg;
        int input[]=new int[5];
        System.out.print("Enter Values : ");
        for(int i=0;i<input.length;i++)
        {
            input[i]=scan.nextInt();   
        }
        avg=getsum(input);
        System.out.println("Sum is : "+avg);
    }
    public static double  getsum(int[] arr)   
    {
        double sum=0;
        for(int j=0;j<arr.length;j++)
        {
            sum=arr[j]+sum;
        }   
        return sum;
    }

}

No comments:

Post a Comment