java help?

java help?

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

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:
ModuleNotFoundError: No module named 'helpy'
ModuleNotFoundError: No module named 'helpe'
Advertisements
Please HELPP
Helps with combo box and frame
Dojo: How MenuItem helps page redirction - Ajax
Java binary to decimal
While Loop Statement in java 7
Example to create Interface in java
Compare two char array in java
Setting the close button operation for the frame in Java
What is Polymorphism in Java?
Java Training
Java Get Last Line of File
Java Debugger
JDBC Connectivity Code In Java
Java beginners Course
Java Debugger
Java Programming Training
Java Debugger
The Java Applet Viewer
The Java Applet Viewer
Java Programming video tutorial for beginners
Online Java Courses
Introduction to Java Arrays
Java EE Training
Java Tutorial Videos
Java Training Free
Easiest way to learn Java
Java beginners tutorial
Java error cannot read
OOPs concepts in Java
Marvellous chance to learn java from the Java Experts
The Java Applet Viewer
DOM API - Java Beginners
Core Java Training
Java Beginners Guide
Mobile Number Validation Form in JavaScript
Factorial Program in Java
Basic Java programming for Beginners
FTP Server: Get Buffer size
Hibernate configuration file
Getting the Log Level of a Logger
JavaScript array of numbers
Java basics
Java Tutorial for Beginners
Palindrome program in Java
Threads in Java
Analysis tool - SourceGlider for Eclipse
Struts Tutorial
Java - Copying one file to another

Ads