Create an array of String variables and initialize the array with the names of the months from January to December. Create an array containing 12 random decimal values between 0.0 and 100.0. Display the names of each month along with the corresponding decimal value. Calculate and display the average of the 12 decimal values.
Output of Above Java Program
In Month January number is 2.62329136050351
In Month February number is 95.49823339063055
In Month March number is 58.978051814545786
In Month April number is 99.2117855806428
In Month May number is 49.808337007547145
In Month June number is 79.15664466990732
In Month July number is 13.451240158382593
In Month August number is 46.94383635492937
In Month September number is 83.73174198408276
In Month October number is 88.0275024074453
In Month November number is 10.300025142868542
In Month December number is 91.70504925022092
Average of values in numbers is 59.95297826014221
public class Months { public static void main(String args[]) { // Initialize the months' array with names of the months of the year: String[] monthNames = { "January" , "February", "March" , "April", "May" , "June" , "July" , "August", "September","October" , "November", "December" }; double average = 0.0; // A variable used to find the average. // Declare an array which can contain the 12 random numbers: double[] numbers = new double[12]; // Fill in the numbers' array with 12 numbers between 0.0 and 100.0, and print it out, // alongside each month. Also keep track of the sum of those numbers elements. for(int i = 0 ; i < numbers.length ; ++i) { numbers[i] = 100.0*Math.random(); System.out.println("In Month " + monthNames[i] + " number is " + numbers[i]); average += numbers[i]; } average /= numbers.length; System.out.println("\nAverage of values in numbers is " + average); } }
Output of Above Java Program
In Month January number is 2.62329136050351
In Month February number is 95.49823339063055
In Month March number is 58.978051814545786
In Month April number is 99.2117855806428
In Month May number is 49.808337007547145
In Month June number is 79.15664466990732
In Month July number is 13.451240158382593
In Month August number is 46.94383635492937
In Month September number is 83.73174198408276
In Month October number is 88.0275024074453
In Month November number is 10.300025142868542
In Month December number is 91.70504925022092
Average of values in numbers is 59.95297826014221