Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //class work
- import java.util.Scanner; //importing scanner
- public class number5And6 { //program begins
- public static void main(String[] args) { //main
- Scanner input = new Scanner (System.in); //scanner declaration
- int [] arr = new int [5]; //int array declaration
- for (int i=0; i<arr.length; i++) { //for loop to initialize the variables within the array
- System.out.println("Enter a number: ");
- arr[i] = input.nextInt();
- } //for loop ends
- cubeArray(arr); //calls the method to change the data within array
- for(int i=0; i<arr.length; i++) { //for loop to print the updated values
- System.out.printf("Cubed values: %d\n", arr[i]);
- } //for loop ends
- //output statement for the average of the array
- System.out.println("The average of the array is: " +average(arr));
- } //main method
- //method called cubeArray, takes on an argument which is an array and changes the values within the array to its cubed equivalent
- public static void cubeArray(int [] cube) {
- for(int j=0; j<cube.length; j++) {
- cube[j] = cube[j] * cube[j] * cube[j];
- }
- } //method cubeArray ends
- //method called average, adds all the values in the array and finds the average
- public static int average(int [] array) {
- int average = 0, sum = 0; //variable declaration
- for(int k=0; k<array.length; k++) {
- //finding the sum of the array
- sum+=array[k];
- }
- //finding the average of the array
- average = sum / array.length;
- return average;
- } //method average ends
- } //program ends
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement