Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package md.WK14toWK18;
- import java.text.DecimalFormat;
- import java.util.Arrays;
- public class number04 {
- public static void main(String[] args) {
- int[] numbers = new int[]{3, 4, 5, 7, 9};
- System.out.println("Original number array: " + Arrays.toString(numbers));
- //calculate sum of all the array elements
- double sum = 0;
- for (int i = 0; i < numbers.length; i++)
- sum = sum + numbers[i];
- //calculate average
- double average = sum / numbers.length;
- //Shows only 2 decimal format
- DecimalFormat df = new DecimalFormat("#.##");
- //Print the Average in 2 decimal format
- System.out.print("The average value of the array elements is :");
- System.out.print(df.format(average));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement