# Java program to find average of two numbers

In this program we will find the average of two numbers. To find average of two numbers, they are added and their sum is divided by two.

Problem:
Input two numbers from the user and display their average on the screen.

Code:

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18``` ``` /** * @author Hafiz Muhammad Umer */ import java.util.Scanner; public class Average { public static void main(String[] args) { Scanner input=new Scanner(System.in); double a,b,avg; System.out.print("Enter first number: "); a=input.nextDouble(); System.out.print("Enter second number: "); b=input.nextDouble(); avg=(a+b)/2; System.out.println("The average of "+a+" and "+b+" is "+avg); } } ```

Output:

```Enter first number: 1
Enter second number: 2
The average of 1.0 and 2.0 is 1.5
```

Explanation:

• The line 4 imports the Scanner utility in java. It will be used to create Scanner objects so that we can take input in our program.
• Line 5 defines a class which ends at line 18.
• Line 7 defines the main method which ends at line 17.
• Line 9 creates a Scanner object named input.
• Line 10 defines three double variables.
• From line 11 to 14, two values are taken from the user as input.
• At line 15, the average of a and b is calculated and stored in the variable avg.
• At line 16, the average is displayed on the screen.