Java program to calculate average of 3 numbers

Let’s learn java program to calculate average of 3 numbers.

Java program to calculate average of 3 numbers

To find or compute average of 3 numbers in java we are taking input from user using nextDouble() method of Scanner class and storing in three variables number1, number2 and number3.

These variables are then passed to calculateAverage() method to find average using “/” division operator.

Here’s the java program.

import java.util.Scanner;
public class AverageNumberDemo 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);
      System.out.print("Please enter first number: ");
      double number1 = sc.nextDouble();
      System.out.print("Please enter second number: ");
      double number2 = sc.nextDouble();
      System.out.print("Please enter third number: ");
      double number3 = sc.nextDouble();
      System.out.print("The average of 3 numbers is: " + calculateAverage(number1, number2, number3));
      sc.close();
   }
   public static double calculateAverage(double num1, double num2, double num3)
   {
      return (num1 + num2 + num3) / 3;
   }
}


Output:

Please enter first number: 226
Please enter second number: 256
Please enter third number: 655
The average of 3 numbers is: 379.0


Also read – Quicksort java