1// Java program to calculate average of 3 numbers
2import java.util.Scanner;
3public class AverageNumberDemo
4{
5 public static void main(String[] args)
6 {
7 Scanner sc = new Scanner(System.in);
8 System.out.print("Please enter first number: ");
9 double number1 = sc.nextDouble();
10 System.out.print("Please enter second number: ");
11 double number2 = sc.nextDouble();
12 System.out.print("Please enter third number: ");
13 double number3 = sc.nextDouble();
14 System.out.print("The average of 3 numbers is: " + calculateAverage(number1, number2, number3));
15 sc.close();
16 }
17 public static double calculateAverage(double num1, double num2, double num3)
18 {
19 return (num1 + num2 + num3) / 3;
20 }
21}
1int a, b, c, d;
2 a = 2;
3 b = 3;
4 c = 5;
5 d = 10;
6 System.out.println("Average of n nubers is: " + (a + b + c + d) / 4);
7 // OR
8 Scanner scanner = new Scanner(System.in);
9 System.out.println("Enter 1st number: ");
10 int firstNumber = scanner.nextInt();
11 System.out.println("Enter 2nd number: ");
12 int secondNumber = scanner.nextInt();
13 System.out.println("Enter 3rd number: ");
14 int thirdNumber = scanner.nextInt();
15 System.out.println("Enter 4th number: ");
16 int fourthNumber = scanner.nextInt();
17 System.out.println("Enter 5th number: ");
18 int fifthNumber = scanner.nextInt();
19 System.out.println("Enter 6th number: ");
20 int sixthNumber = scanner.nextInt();
21 System.out.println("Enter Nth number: ");
22 int nthNumber = scanner.nextInt();
23 scanner.close();
24 System.out.println("Average of abouve input numbers is: "
25 + (firstNumber + secondNumber + thirdNumber + fourthNumber + fifthNumber + sixthNumber + nthNumber)
26 / 7.0);