Calculate Average of 3 Numbers in Java
Here you will get example code to calculate the Average of 3 Numbers in Java programming. For calculating average, we needed 3 number values from the user. Here we will use scanner class to get input from the user.
Program Code: Average of 3 Numbers in Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | //Average of 3 Numbers in Java import java.util.Scanner; public class AverageCalc { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the first number: "); double n1 = scanner.nextDouble(); System.out.print("Enter the second number: "); double n2 = scanner.nextDouble(); System.out.print("Enter the third number: "); double n3 = scanner.nextDouble(); double average = (n1 + n2 + n3) / 3; System.out.println("The average of given numbers is: " + average); scanner.close(); } } |
Output
C:\CodeRevise\java>javac AverageCalc.java
C:\CodeRevise\java>java AverageCalc
Enter the first number: 10
Enter the second number: 20
Enter the third number: 30
The average of given numbers is: 20.0
Check out our other Java Programming Examples