In this article, you will learn how to find the average of three given numbers and write its program logic in java language. The average of the three numbers is the sum of three numbers divided by 3. Let’s assume a, b, and c are three numbers. Now see the calculation below:
The average of three numbers = ( The sum of given three numbers ) / 3
Average = ( a + b + c ) / 3
For example:
a=2; b=5; c=11
Average => a+b+c/3 => 2+5+11/3 => 18/3 => 6
Below is the java program to find the average of three given numbers,
package com.javacodepoint.basics;
import java.util.Scanner;
public class AverageOfThreeNumbers {
// main method
public static void main(String[] args) {
// declaring num1 & num2 & num3 variables to take three numbers, sum variable to
// calculate sum, and average to calculate average
float num1, num2, num3, sum, average;
// Scanner object for reading numbers from user input
Scanner sc = new Scanner(System.in);
// reading first number
System.out.println("Enter the first number: ");
num1 = sc.nextFloat();
// reading second number
System.out.println("Enter the second number: ");
num2 = sc.nextFloat();
// reading third number
System.out.println("Enter the third number: ");
num3 = sc.nextFloat();
// calculating the sum of given all three numbers
sum = num1 + num2+ num3;
// calculating the average of the given three numbers
average = sum / 3;
// Printing the calculated average
System.out.println("The Average of the given three Numbers= " + average);
}
}
OUTPUT:
Enter the first number:
2
Enter the second number:
5
Enter the third number:
11
The Average of the given three Numbers= 6.0