int [ ] array = { 2, 4, 6, 8, 1, 3, 5, 7}; int george = 0; for (int i = 0; i < array.length; i++) george += array[i]; System.out.print(george);
Question
int [ ] array = { 2, 4, 6, 8, 1, 3, 5, 7}; int george = 0; for (int i = 0; i < array.length; i++) george += array[i]; System.out.print(george);
Solution
The code you provided is written in Java. It declares an array of integers, then uses a for loop to sum up all the elements in the array. The sum is stored in the variable 'george'. Finally, it prints out the value of 'george'.
Here's a step-by-step breakdown:
-
An integer array named 'array' is declared with the elements 2, 4, 6, 8, 1, 3, 5, 7.
-
An integer variable named 'george' is declared and initialized with the value 0.
-
A for loop is started which runs from i = 0 to i = array.length (which is 8 in this case).
-
Inside the for loop, the value of 'george' is incremented by the value of the current element in the array (array[i]).
-
This process continues until all elements in the array have been added to 'george'.
-
Finally, the value of 'george' is printed out using System.out.print(). This will be the sum of all the elements in the array, which is 36.
Similar Questions
public static void main(String args[]){ int array_variable[ ][ ] = {{1,2,3},{4,5,6},{7,8,9}}; int sum = 0; for ( int i = 0; i < 3; ++i) for( int j = 0; j < 3 ; ++j) sum = sum + array_variable[i][j]; System.out.print(sum / 5); }}
What is the output of this program? int array[] = new int [5]; for (int i = 5; i > 0; i--) array[5 - i] = i; Arrays.sort(array); for (int i = 0; i < 5; ++i) System.out.print(array[i]);
Select the correct answerWhat will be the output of the following Java code?class array_output { public static void main(String args[]) { int array_variable[][] = {{ 1, 2, 3}, { 4 , 5, 6}, { 7, 8, 9}}; int sum = 0; for (int i = 1; i < 3; ++i) for (int j = 1; j < 3 ; ++j) sum = sum + array_variable[i][j]; System.out.print(sum / 6); } }Options8413
What is the output of the following code?public class array_output { public static void main(String args[]) { int array_variable[] = new int[10]; for (int i = 0; i < 10; ++i) { array_variable[i] = i; System.out.print(array_variable[i] + " "); i++; } }}Options: Pick one correct answer from below0 2 4 6 81 3 5 7 90 1 2 3 4 5 6 7 8 91 2 3 4 5 6 7 8 9 10
for (int i = 0; i < arr.length-1; i++){ for (int j = i+1; j < arr.length; j++) { if( (arr[i].equals(arr[j])) && (i != j) ) { System.out.println(arr[i]); } }}
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.