In this tutorial, you will learn how to write a Java program to find the average of three numbers. First, we will understand the formula for calculating the average of three numbers.
Next, we will write a simple Java program by assigning values directly to variables. After that, we will create an interactive Java program using the Scanner class to accept three numbers from the user and calculate their average.
Understanding the Average of Three Numbers
The average of three numbers is calculated by adding all three numbers together and then dividing the sum by 3.
The general formula to calculate the average of three numbers is:
Average = (Number 1 + Number 2 + Number 3) / 3
Where:
- Number 1 = First number
- Number 2 = Second number
- Number 3 = Third number
- Average = Average of the three numbers
Let’s Walk Through an Example
Imagine you want to find the average of three everyday numbers: 10, 20, and 30.
Using the mathematical formula:
Average = (10 + 20 + 30) / 3 Average = 60 / 3 Average = 20
Therefore, the average of 10, 20, and 30 is 20.0.
Java Program to Find Average of Three Numbers Using Pre-assigned Values
Problem Statement: Write a Java program to find the average of three numbers by initializing the values directly to variables.
Complete Java Program
// Java program to find the average of three numbers.
public class AverageOfThreeNumbers {
// The main() method of the program.
public static void main(String[] args) {
// Declare three variables, number1, number2, and number3, of type double, and assign the three numbers to them.
double number1 = 10.0;
double number2 = 20.0;
double number3 = 30.0;
// The expression (number1 + number2 + number3) calculates the sum of the three numbers,
// and the sum is divided by 3 to calculate the average.
// The average variable stores the calculated result.
double average = (number1 + number2 + number3) / 3;
// System.out.println() displays the numbers and their average
System.out.println("First Number: " + number1);
System.out.println("Second Number: " + number2);
System.out.println("Third Number: " + number3);
System.out.println("Average: " + average);
}
}Expected Output:
First Number: 10.0 Second Number: 20.0 Third Number: 30.0 Average: 20.0
In this Java program:
- We have declared three double variables, number1, number2, and number3, to store the three numbers.
- The expression (number1 + number2 + number3) calculates the sum of the three numbers.
- The sum is divided by 3 to calculate the average.
- The average variable stores the calculated result.
System.out.println()statement displays the numbers and their average.
The most important statement in the program is:
double average = (number1 + number2 + number3) / 3;
This statement first adds the three numbers and then divides their sum by 3.
Java Program to Find Average of Three Numbers Using Scanner
Problem Statement: Write a Java program to find the average of three numbers by taking user input dynamically through the Scanner class and implementing a separate user-defined method to handle the calculation logic.
Complete Java Program
// Java program to calculcate the average of three numbers using Scanner class.
import java.util.Scanner;
public class AverageCalculator {
// Method to calculate the average of three numbers
public static double calculateAverage(double num1, double num2, double num3) {
return (num1 + num2 + num3) / 3;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Accepting input from the user
System.out.println("Enter the first number: ");
double num1 = scanner.nextDouble();
System.out.println("Enter the second number: ");
double num2 = scanner.nextDouble();
System.out.println("Enter the third number: ");
double num3 = scanner.nextDouble();
// Calling the method and storing the result
double average = calculateAverage(num1, num2, num3);
// Displaying the final result
System.out.println("The average is: " + average);
// Closing the scanner resource
scanner.close();
}
}Expected Output:
Enter the first number: 10 Enter the second number: 20 Enter the third number: 30 The average is: 20.0
In this Java program:
- The program imports the built-in Scanner class to enable reading input directly from the console.
- We have created a user-defined static method, calculateAverage(), to accept three numeric values, compute their total sum, divide that sum by three, and return the final calculated average value.
- The main() method acts as the primary entry point where execution begins.
- Inside the main() method, we have invoked the method by passing arguments and stored the returned value in a variable named average of type double.
- After that, we printed the result on the console using the System.out.println() statement.
- Finally, we closed the scanner to prevent resource leaks.







