🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
In this tutorial, we'll create a Java program to calculate the average of a set of numbers using arrays. Arrays in Java work as containers that hold a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.
2. Program Steps
1. Define the main class named AverageCalculator.
2. Inside the main class, define the main method.
3. Inside the main method, declare an array of numbers for which we want to calculate the average.
4. Calculate the sum of the numbers in the array.
5. Calculate the average by dividing the sum by the length of the array.
6. Print the calculated average.
3. Code Program
public class AverageCalculator { // 1. Defining the main class
public static void main(String[] args) { // 2. Defining the main method
// 3. Declaring an array of numbers
int[] numbers = {10, 20, 30, 40, 50};
int sum = 0; // Initializing sum
// 4. Calculating the sum of the numbers in the array
for (int number : numbers) {
sum += number;
}
// 5. Calculating the average
double average = (double) sum / numbers.length;
// 6. Printing the average
System.out.println("The average of the given numbers is: " + average);
}
}
Output:
The average of the given numbers is: 30.0
4. Step By Step Explanation
Step 1: The main class named AverageCalculator is defined.
Step 2: The main method, which is the entry point of the program, is defined inside the main class.
Step 3: Inside the main method, we declare an array numbers containing the elements {10, 20, 30, 40, 50} for which we want to calculate the average.
Step 4: We initialize a variable sum to 0 and then iterate over each number in the numbers array, adding it to sum.
Step 5: We calculate the average by dividing the sum by the length of the numbers array. We cast sum to double to perform floating-point division and get a more accurate result.
Step 6: Finally, the program prints the calculated average 30.0 to the console.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment