0% found this document useful (0 votes)
8 views2 pages

Cahp Classwork

average calc java

Uploaded by

jikitacwb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Cahp Classwork

average calc java

Uploaded by

jikitacwb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

Scanner;

public class Program1 {

//3double val ret as array

public static double[] inputValues() {

Scanner scanner = new Scanner(System.in);

double[] values = new double[3];

// loop 3 val

for (int i = 0; i < 3; i++) {

System.out.print("Enter value " + (i + 1) + ": ");

values[i] = scanner.nextDouble();

return values; // ret array

// calc avg 3 val

public static double calculateAverage(double[] values) {

double sum = 0;

for (double value : values) {

sum += value; // add value sum

return sum / values.length; // ret avg

}
// print avg val

public static void printAverage(double average) {

System.out.printf("The average of the entered values is: %.2f\n",


average); // Print the average with 2 decimal places

public static void main(String[] args) {

//call input val

double[] values = inputValues();

//calc avg

double average = calculateAverage(values);

//call print avg and display

printAverage(average);

You might also like