- Problem Statement
- Our Logic to calculate the Average of Array
- What is the average of the numbers?
- Algorithm to calculate average of the numbers
- Program 1: Java Program to Calculate Average of Integer Array
- Program 2: Average of numbers in array using for loop
- Program 3: Average of numbers in array using while loop
- Program 4: Average of array elements using Java 8
- Conclusion
In this tutorial, we are going to learn a writing java program to find the average of given numbers. In this program basically, we will not take any user input. We have an integer array so we will iterate the integer array and then calculate the average number.
Problem Statement
For any given sequence of predefined numbers, we have to calculate the average of the numbers (none of the values are taken from the user).
For example
Case1: If the predefined sequence 1,2,3,4,5,6,7,8,9.
The output should be 5.
Case2: If the predefined sequence 11,22,33,44,55,66,77,88,99.
The output should be 55.
Our Logic to calculate the Average of Array
- Our program will declare the variable sum =0, i=0 and also the array is predefined at the beginning.
- Then. Our program will calculate the sum of all the numbers using ‘while loop’.
- Finally, the Calculated sum is then divided by the number of elements present in the array (or length of the array) and print the result as the output.
What is the average of the numbers?
Average also called the arithmetic mean, is calculated by adding a group or collection of numbers and then dividing that sum by the number of elements of the same group or collection.
Algorithm to calculate average of the numbers
Step 1: Start
Step 2: float sum =0, int i=0.
Step 3: int [] arr = {1,2,3,4,5}
Step 4: while (I < arr.length) { sum = sum+arr[i]; }
Step 5: assign a variable, avg as float average = sum/arr.length;
Step 6: print average
Step 7: Stop
Program 1: Java Program to Calculate Average of Integer Array
import java.util.*;
public class Main
{
public static void main(String[] args) {
float sum=0;
int i=0;
int[] arr = {1, 2, 3, 4, 5};
while(i<arr.length) {
sum=sum+arr[i];
i++;
}
float average = sum/arr.length;
System.out.println("Average of Array Integer is : "+average);
}
}
Output
Average of Array Integer is : 3.0
Explanation
In this example, the predefined array is {1, 2, 3, 4, 5}. Where the size of array is 5 so the average will be
Average = (1+2+3+4+5)/ 5
The output generated is 3.0.
Program 2: Average of numbers in array using for loop
In this Average program we have an array and we are taking values at run time from the user. For taking the values at run time we are using the Scanner. Also In this program we are using for loop concept to iterate the array.
import java.util.*;
public class Main
{
public static void main(String[] args) {
float sum=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of array:");
int n = sc.nextInt();
float arr[] = new float[n];
System.out.println("Enter " + n + " array elements: ");
for(int i=0; i<n; i++) {
arr[i] = sc.nextInt();
sum=sum+arr[i];
}
float average = sum/n;
System.out.println("Average of number is : "+average);
}
}
Output
Enter 5 array elements:
34
12
33
56
23
Average of number is : 31.6
Program 3: Average of numbers in array using while loop
In this Average program we have we are taking size and values at runtime from the user for array. For taking the values at runtime we are using the Scanner Class. Also In this program we are using while loop concept to iterate the array.
import java.util.*;
public class Main
{
public static void main(String[] args) {
float sum=0;
int i=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of array: ");
int n = sc.nextInt();
float arr[] = new float[n];
System.out.println("Enter " + n + "array elements: ");
while(i<n) {
arr[i] = sc.nextInt();
sum=sum+arr[i];
i++;
}
float average = sum/n;
System.out.println("Average of numbers are :"+average);
}
}
Output
Enter 5array elements:
12
33
45
23
67
Average of numbers are :36.0
Program 4: Average of array elements using Java 8
import java.util.*;
public class Main
{
public static void main(String[] args) {
float sum=0;
int i=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of array: ");
int n = sc.nextInt();
int arr[] = new int[n];
System.out.println("Enter " + n + "array elements: ");
while(i<n) {
arr[i] = sc.nextInt();
i++;
}
Double average = Arrays.stream(arr).average().getAsDouble();
System.out.println("Average of numbers are :"+average);
}
Output
Enter the size of array:
5
Enter 5array elements:
34
55
12
67
32
Average of numbers are :40.0
With the help of above example, I hope you are now able to write program using java to calculate the average of numbers.
Conclusion
In this tutorial you have seen multiple ways to write a program to calculate the average of an array in Java. We have seen using for loop, while loop and Java 8. The logic behind each program involved obtaining user inputs through arrays, summing up the elements, and then dividing the sum by the number of elements to find the average. The tutorial covered three main methods: using a while loop, a for loop, and Java 8 streams.
The first program demonstrated a simple approach with a predefined array, while the second and third programs allowed users to input values at runtime, utilizing the Scanner class for user interaction. The fourth program showcased a modern approach using Java 8 streams, simplifying the process with a concise and efficient solution.
Through these examples, you’ve gained a solid understanding of various methods to find the average of an array in Java. Whether you prefer traditional loops or the more streamlined Java 8 approach, you now have the knowledge to implement the solution that best fits your coding style and requirements.
Happy coding!
Similar Reads
-
Find middle element of a linked list in single pass
Linked lists are a fundamental data structure used in computer science for organizing and managing data. A linked list is… -
Python Program to Find Last 3rd element in Singly Linked List
In this tutorial, we are going to learn the writing python program to Find 3rd element of Linked List from… -
Most important JavaScript Interview Questions To Prepare
In this Page we have collected and explained Most important Javascript Interview Questions and Answers for begineers, freshers as well… -
Sum of digits of Given Number in Java
In this tutorial we will learn writing Java program to calculate the sum of its digit. We will also see… -
Hibernate Interview Questions for 2+ years of experience
Certainly! Here's a list of commonly asked interview questions on Hibernate for candidates with 2+ years of experience: Basic Hibernate… -
68 Most Important Microservices Interview Questions
Certainly, here's an extended list of 50 commonly asked interview questions on microservices for candidates with 2+ years of experience:… -
60 Most Important Git Interview Questions
Certainly! Here is a list of commonly asked interview questions on Git for candidates with fresher or having of experience… -
50+ Most important Java Interview Questions for 5+ Years Exp
1. Explain the SOLID principles in Java. Provide examples of how you have applied these principles in your projects. SOLID… -
60+ Spring Boot interview questions for 4+ years Exp.
1. What is Spring Boot and how does it differ from the Spring framework? Spring Boot is a framework designed… -
60+ Mostly Asked Spring Boot Interview Questions for 3+ Yrs
Here is a list of 60+ Spring Boot interview questions for candidates with 3+ years of experience: 1. What is…