Advertisement
ralphdc09

itf103.finals-wk-14-wk-18.number04

Jun 9th, 2021
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.79 KB | None | 0 0
  1. package md.WK14toWK18;
  2.  
  3. import java.text.DecimalFormat;
  4. import java.util.Arrays;
  5.  
  6. public class number04 {
  7.     public static void main(String[] args) {
  8.  
  9.         int[] numbers = new int[]{3, 4, 5, 7, 9};
  10.         System.out.println("Original number array: " + Arrays.toString(numbers));
  11.         //calculate sum of all the array elements
  12.         double sum = 0;
  13.         for (int i = 0; i < numbers.length; i++)
  14.             sum = sum + numbers[i];
  15.         //calculate average
  16.         double average = sum / numbers.length;
  17.         //Shows only 2 decimal format
  18.         DecimalFormat df = new DecimalFormat("#.##");
  19.         //Print the Average in 2 decimal format
  20.         System.out.print("The average value of the array elements is :");
  21.         System.out.print(df.format(average));
  22.     }
  23. }
  24.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement