Java Program to find the Average of Three Numbers

Write a Java program to find the average of three numbers using the + and / arithmetic operator. First, it accepts three numbers of double type and then uses the arithmetic plus to add them and / operator to find the average.

package RemainingSimplePrograms;

import java.util.Scanner;

public class AvgOfThree1 {
	private static Scanner sc;
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Please Enter the first Number = ");
		double num1 = sc.nextDouble();
		
		System.out.print("Please Enter the Second Number = ");
		double num2 = sc.nextDouble();
		
		System.out.print("Please Enter the Third Number = ");
		double num3 = sc.nextDouble();
		
		double average = (num1 + num2 + num3)/ 3;
		System.out.println("\nThe Average of Three Numbers = " + average);
	}
}
Java Program to find the Average of Three Numbers

In this Java example, we created a calAvg function that finds and returns three numbers’ average.

package RemainingSimplePrograms;

import java.util.Scanner;

public class AvgOfThree2 {
	private static Scanner sc;
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Please Enter the first = ");
		double num1 = sc.nextDouble();
		
		System.out.print("Please Enter the Second = ");
		double num2 = sc.nextDouble();
		
		System.out.print("Please Enter the Third = ");
		double num3 = sc.nextDouble();
		
		double average = calAvg(num1, num2, num3);
		System.out.println("\nThe Average = " + average);
	}
	
	public static double calAvg(double a, double b, double c) {
		return (a + b + c)/3;
	}
}
Please Enter the first = 99
Please Enter the Second = 124.7
Please Enter the Third = 33.89

The Average = 85.86333333333333

In the program below, we declare a function that accepts three numbers to calculate the average and returns the result. Refer to Java Arithmetic operators.

public class example {
public static double calculateAVG(double a, double b, double c) {
return (a + b + c) / 3;
}
public static void main(String[] args) {
double a = 25.50, b = 100, c = 50.50;

System.out.format("Average = %.2f", calculateAVG(a, b, c));
}
}
Average = 58.67

Also Read