**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 calculate the sum and average of 3 (three) 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 three numbers,

1) Take three numbers

2) Declare a sum variable

3) Calculate the addition of those three numbers and assign them to the sum variable

4) Find average as average = sum/3

5) Finally, display the result of the average value

**Java Program to Calculate sum and average of 3 Numbers**

```
public class Average {
public static void main(String[] args) {
// take two numbers
double num1 = 10;
double num2 = 15;
double num3 = 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 + num3;
// calculate the average value
avg = sum/3;
// 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 Calculate Sum and Average of 3 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 three numbers
double num1 = 0;
double num2 = 0;
double num3 = 0;
// declare sum variable
// and initialize with 0
double sum = 0.0;
// declare average variable
double avg = 0.0;
// take three numbers
System.out.print("Enter three numbers: ");
num1 = scan.nextDouble();
num2 = scan.nextDouble();
num3 = scan.nextDouble();
// calculate the sum value
sum = num1 + num2 + num3;
// calculate the average value
avg = sum/3;
// display result
System.out.println("Average: " + avg );
}
}
```

Output for the different test-cases:-

Enter three numbers: 15 25 35

Average: 25.0

Enter three numbers: 10 25 50

Average: 28.333333333333332

In the above program, first, we had declared all the variables. The num1, num2, and num3 variables will hold/store the input values. The sum variable is declared and initialized with 0, average variable is also declared.

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. 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

Now, we had taken three numbers as input and calculated the sum value as sum = num1 + num2 + num3. Then we calculated the average value as avg = sum / 3. Finally, we got the average value and it is displayed on the screen.

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