**Java Basic Program**

➤ Hello World program

➤ Java Addition Program

➤ Average of 2 Numbers

➤ Average of 3 Numbers

➤ Simple Interest in Java

➤ Compound Interest

➤ Display ASCII Value

➤ Area of Circle Java

➤ Area of Rectangle

➤ Java Area of Triangle

➤ Java Swap 2 Numbers

➤ Distance b/w 2 Points

**Java Flow Control Program**

**Java Array Programs**

**Java String Programs**

Program Description:- **Write a Java program to find the average of two numbers. **

First, we will develop a simple Java program by hardcoding the values i.e. required values will be initialized inside the program.

Procedure to find the average of two numbers,

1) Take two numbers

2) Declare a `sum`

variable

3) Calculate the addition of two numbers and assign them to the `sum`

variable

4) Find average as `average = sum/2`

5) Finally, display the result of the average value

**Java program to find average of two numbers**

```
public class Average {
public static void main(String[] args) {
// take two numbers
double num1 = 10;
double num2 = 20;
// declare sum variable
// and initialize with 0
double sum = 0.0;
// declare average variable
double avg = 0.0;
// calculate the sum value
sum = num1 + num2;
// calculate the average value
avg = sum/2;
// display result
System.out.println("Average: " + avg );
}
}
```

Output:-

Average: 15.0

In the above program, we have hardcoded the input values. Now, let us develop another Java program to do the same but take input value from the end-user.

**Java program to find average of two numbers by taking input from end-user**

```
import java.util.Scanner;
public class Average {
public static void main(String[] args) {
// create Scanner class object
Scanner scan = new Scanner(System.in);
// declare two numbers
double num1 = 0;
double num2 = 0;
// declare sum variable
// and initialize with 0
double sum = 0.0;
// declare average variable
double avg = 0.0;
// take two numbers
System.out.print("Enter two numbers: ");
num1 = scan.nextDouble();
num2 = scan.nextDouble();
// calculate the sum value
sum = num1 + num2;
// calculate the average value
avg = sum/2;
// display result
System.out.println("Average: " + avg );
}
}
```

Output for the different test-cases:-

Enter two numbers: 50 100

Average: 75.0

Enter two numbers: -100 125

Average: 12.5

In the above program, first, we had declared all the variables. The num1 and num2 variables will hold/store the input values. The sum variable is declared and initialized with 0, average variable is also declared. The num1, num2, sum, and average variables are of double data type and can hold integer and floating-point values. See more about float and double data types in Java

To take the input value from the end-user we have used the Scanner class which is defined in the java.util.Scanner package but you can also take any input-output stream class like BufferedReader class to read the input.

Now, we had taken two numbers as input and calculated the sum value as sum = num1 + num2. Then we calculated the average value as avg = sum / 2. Finally, we got the average value and it is displayed on the screen. If you are finding difficulty extracting the average of two or three numbers, the best solution is to use the average calculator.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Also See:- Java Hello World program, Java Addition Program, Calculate Simple Interest, Calculate Compound Interest, Display ASCII value in Java