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 average numbers
Java program to average numbers
Output:
How many numbers: 5
Enter the numbers:
1.0 2.0 3.0 4.0 5.0
The average of 5 numbers is 3.0
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; float average; float sum=0,x; System.out.println("How many numbers:"); n = in.nextInt(); System.out.println("Enter the numbers:"); for(int i=1;i<=n;i++) { x = in.nextFloat(); sum += x; } /* calculating the average sum of elements average = --------------- number of elements */ average = sum / (float)n; System.out.println("The average of "+n+" numbers is "+average); } }
PREVIOUS
NEXT
HOME