Latest Tutorials| Questions and Answers|Ask Questions?|Site Map




Have Programming Question? Ask it here!
 
 
 


Casper
java help?
1 Answer(s)      3 years and 8 months ago
Posted in : Java Beginners


Write a program, where you first ask values to an array with 6 integer values and then count the sum and average of the values in methods (send in the array & return the counted value to the main program). Print out results in the main program


Advertisement
View Answers

December 1, 2010 at 11:04 AM


Hi Friend,

Try the following code:

import java.util.*;
class  NumberExample{
    public static int sum(int arr[]){
        int sum=0;
    for(int i=0;i<arr.length;i++){
    sum+=arr[i];
    }
    return sum;
    }
    public static int average(int arr[]){
    int sum=0,avg=0;
    for(int i=0;i<arr.length;i++){
    sum+=arr[i];
    }
    avg=sum/arr.length;
    return avg;
    }
    public static void main(String[] args) 
    {
        int array[]=new int[6];
        Scanner input=new Scanner(System.in);
        System.out.println("Enter 6 elements: ");
        for(int i=0;i<array.length;i++){
            array[i]=input.nextInt();
        }
        System.out.println("Sum of Array Elements: "+sum(array));
        System.out.println("Average of Array Elements: "+average(array));
    }
}

Thanks



Related Tutorials/Questions & Answers:
Advertisements
 

 

 

DMCA.com