94.7k views
4 votes
You do not need to use any functions beyond the main function in this problem. Initialize an array of int with the values: 4, 6, 9, and 12. Write a for loop to add the values in the array and find their sum. Write a second loop to print the values from the array and their sum in the following format: 4 6 9 12

1 Answer

5 votes

Answer:

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

int[] arr = {4, 6, 9, 12};

int sum = 0;

for(int i=0; i<arr.length; i++){

sum += arr[i];

}

for(int i=0; i<arr.length; i++){

System.out.print(arr[i] + " ");

}

System.out.println("");

System.out.println("The sum of the numbers: " + sum);

}

}

Step-by-step explanation:

* The code is written in Java.

- Initialize the array with the given numbers

- Initialize the sum variable as zero

- Calculate the sum in the first for loop

- Print the numbers in the second for loop

- Print the sum

User Szydan
by
4.2k points