Introduction :
This Java program, named Array Sum And Average, is designed to calculate the sum and average of elements in an array. It interacts with the user to obtain the size of the array and prompts the user to input the array elements. The program then performs the necessary calculations and displays the sum and average to the user.
Algorithm :
- Import Scanner Class:
- The program begins by importing the Scanner class from the util package. This class is essential for obtaining user input.
- Get Array Size:
- The program prompts the user to enter the size of the array using the Scanner The entered size is stored in the variable size.
- Create Array:
- An array named numbers is declared with a size equal to the user-specified size. This array will store the elements entered by the user.
- Input Array Elements:
- A for loop is used to iterate through the array, prompting the user to input each element. The elements are stored in the array.
- Calculate Sum:
- Another for loop is used to calculate the sum of all elements in the array. The sum is stored in the variable sum.
- Calculate Average:
- The average is calculated by dividing the sum by the size of the array. The result is stored in the variable average.
- Display Results:
- The program prints the calculated sum and average to the console, providing the user with the final results.
- Close Scanner:
- Finally, the Scanner object is closed to prevent resource leaks.
Explanation :
This program is designed to be user-friendly, allowing the user to easily input the size and elements of the array. The use of loops facilitates the input process and the calculation of the sum. The program demonstrates basic concepts of array handling, user input, and mathematical operations in Java. The displayed results provide a clear output of the sum and average of the entered array elements.
Code :
import java.util.Scanner;
public class ArraySumAndAverage {
public static void main(String[] args) {
// Create a Scanner object for user input
Scanner scanner = new Scanner(System.in);
// Get the size of the array from the user
System.out.print("Enter the size of the array: ");
int size = scanner.nextInt();
// Create an array of the specified size
int[] numbers = new int[size];
// Prompt the user to enter elements for the array
System.out.println("Enter the elements of the array:");
for (int i = 0; i < size; i++) {
System.out.print("Element " + (i + 1) + ": ");
numbers[i] = scanner.nextInt();
}
// Close the scanner to avoid resource leak
scanner.close();
// Calculate the sum of the elements
int sum = 0;
for (int num : numbers) {
sum += num;
}
// Calculate the average
double average = (double) sum / size;
// Display the sum and average
System.out.println("Sum: " + sum);
System.out.println("Average: " + average);
}
}
Input :
Enter the elements of the array:
Element 1: 564
Element 2: 258
Element 3: 963
Element 4: 789
Element 5: 546
Output :
Sum: 3120
Average: 624.0
This program prompts the user to enter the size of the array and then input the elements. It calculates the sum and average of the elements and displays the results.
FAQs:
Q1: Can you provide an example of a Java program on arrays?
A1: Certainly! Here’s a simple Java program on arrays that calculates the sum and average of elements in an array. The user is prompted to input the size and elements of the array.
Q2: How can I write a Java program on arrays to find the maximum element?
A2: You can modify a Java program on arrays to find the maximum element by adding a loop to iterate through the array elements and keeping track of the maximum value.
Q3: Is it possible to create a Java program of array for sorting elements?
A3: Absolutely! You can enhance a Java program on arrays to sort its elements using built-in sorting methods or by implementing your own sorting algorithm.
Q4: How do I write a Java program using array to search for a specific element?
A4: You can modify a Java program using array to include a search functionality
Q5: Can you explain how to use array in a Java program to store user inputs?
A5: Certainly! In a Java program using array to store user inputs, you can create an array to hold the entered values.