java program to find average of n numbers

Solutions on MaxInterview for java program to find average of n numbers by the best coders in the world

showing results for - "java program to find average of n numbers"
Amelie
28 Jan 2018
1// Java program to find average of n numbers
2import java.util.Scanner;
3public class FindAverageOfNNumbers
4{
5   public static void main(String[] args) 
6   {
7      int num, count = 1;   
8      float a, average, total = 0;   
9      Scanner sc = new Scanner(System.in);     
10      System.out.println("Please enter n numbers: ");  
11      num = sc.nextInt();  
12      while(count <= num)
13      {   
14         System.out.println(count + " number: ");  
15         a = sc.nextInt();
16         total += a;
17         ++count;   
18      }
19      average = total / num;   
20      System.out.println("Average is: " + average);
21      sc.close();
22   }
23}
Mila
10 Nov 2019
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);