SCHOOL OF CODE BUILDERS
Learn To CODE. Become A DEVELOPER.
Pages
HOME
DATA STRUCTURES
STRINGS
ARRAYS
MATRIX
BINARY TREES
LINKED LIST
STACK
QUEUE
SORTING
SEARCHING
C
PYTHON
PSEUDOCODE
CONTEST PROBLEMS
ALGORITHMS
PATTERNS
PHP
C PUZZLES
C INTERVIEW QUESTIONS
JAVA
C++
HASHING
RECURSION
BASIC C PROGRAMS
TCS-CODEVITA
FACEBOOK
CONTACT US
Java program to find average of n numbers
Average of n numbers can be calculated using average = (sum of n numbers)/(n).
Output:
How many numbers: 6
Enter the numbers:
1 2 3 4 5 6
The average of 6 numbers is 3.5
Implementation:
import java.util.*; import java.lang.*; import java.io.*; class CodeRegister { public static void main (String[] args) throws java.lang.Exception { Scanner in = new Scanner(System.in); int n,x; float average; int sum=0; System.out.println("How many numbers:"); n = in.nextInt(); System.out.println("Enter the numbers:"); for(int i=1;i<=n;i++) { x = in.nextInt(); sum += x; } /* calculating the average sum of elements average = --------------- number of elements */ average = (float)sum / (float)n; System.out.println("The average of "+n+" numbers is "+average); } }
PREVIOUS
NEXT
HOME