Facebook
From remedy, 2 Weeks ago, written in Java.
Embed
Download Paste or View Raw
Hits: 131
  1. ```java
  2. import java.util.Scanner;
  3.  
  4. public class AverageCalculator {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.  
  8.         // Prompt the user to enter the number of elements
  9.         System.out.print("Enter the number of elements: ");
  10.         int numElements = scanner.nextInt();
  11.  
  12.         // Create an array to store the numbers
  13.         double[] numbers = new double[numElements];
  14.  
  15.         // Prompt the user to enter each number and store them in the array
  16.         System.out.println("Enter " + numElements + " numbers:");
  17.         for (int i = 0; i < numElements; i++) {
  18.             System.out.print("Enter number " + (i + 1) + ": ");
  19.             numbers[i] = scanner.nextDouble();
  20.         }
  21.  
  22.         // Calculate the sum of all numbers in the array
  23.         double sum = 0;
  24.         for (double num : numbers) {
  25.             sum += num;
  26.         }
  27.  
  28.         // Calculate the average
  29.         double average = sum / numElements;
  30.  
  31.         // Print the sum and average
  32.         System.out.println("Average of the numbers: " + average + " and the sum is: " + sum);
  33.  
  34.         // Close the scanner
  35.         scanner.close();
  36.     }
  37. }
  38. ```