• Register
0 votes
53 views

Problem:

I am a novice Java programmer who started learning to program in Java just a few weeks ago. I got an assignment from my tutor in which I have to write a program that prints the sum of the elements of a given array. The output of the program should be as follows:
1 + 2 + 3 + 4 + 5 + 6 = 21

I tried writing the following program, but it gives some strange errors:

public class array_sum{

     public static void main(String []args){
       int[] array = {1,2,3,4,5,6};
       for(int i = 0; i < array.length; i++)
       {
           sum = array[i] + array[i+1];
       }
       
       for(int i = 0; i < array.length; i++)
       {
           System.out.print(array[i]);
       }
       System.out.println(" = " + sum);
     }
}

I am allowed to take help from the Internet so asking for help from you guys. Can anybody please provide me with a solution? Thanks!

10 6 3
6,950 points

Please log in or register to answer this question.

1 Answer

0 votes

Solution:

Hello Code Learner! I appreciate your attempt to the assignment and you have almost done the task. All you need to do is to change the code a little, and you will have the desired output. Read the comments in following code to better understand the solution:

public class array_sum{

     public static void main(String []args){
       int[] array = {1,2,3,4,5,6};   //array of integers
       
       int sum = 0;                  //an integer variable to store the sum of elements
       
       for(int i = 0; i < array.length; i++) //looping through the array
       {
           sum += array[i];  //adding the new element of the array in previous value of sum and assigning it back to sum.
       }
      
       
       for(int i = 0; i < array.length; i++)  //again looping through the array to print its elements
       {
           System.out.print(array[i]);  //printing each element of array
         
           if(i < array.length - 1)   //if true "plus" is printed. If false, else block is executed
           {
               System.out.print(" + ");
           }
           else
           {
               break;   //if i = array.length, the loop breaks
           }
       }
       
       System.out.println(" = " + sum);  //after printing the elements of the array, printing the sum of elements on the screen
     }
}
12 5 4
20,500 points

Related questions

0 votes
1 answer 7 views
7 views
Problem: How can I find the values in an array whose sum equals some given value?
asked Feb 27 Code Learner 7k points
0 votes
1 answer 110 views
110 views
Problem: Beginner here! I have got an assignment in which I have to write a program that finds the sum of array elements taken as input from the user. I have to define a function that calculates the sum and returns the sum. The program should print the sum on the ... logic is not so strong so I just failed to build logic. Could anyone from here help me find the solution to this task? Thanks!
asked Jan 8 Code Learner 7k points
1 vote
2 answers 146 views
146 views
I am trying to find out the sum of the diagonal elements in a matrix. Here, n is the size of the square matrix and a is the matrix. Can someone explain this to me what is happening here. n = 3 a = [[11,2,4],[4,5,6],[10,8,-12]] sum_first_diagonal = sum(a[i][i] for i in range(n)) sum_second_diagonal = sum(a[n-i-1][n-i-1] for i in range(n)) print(str(sum_first_diagonal)+" "+str(sum_first_diag
asked Oct 25, 2020 psandprop 2.4k points
0 votes
1 answer 45 views
45 views
Problem: Using this code we find the sum of diagonal elements of a square matrix.For example, for a 2 x 2 matrix, the sum of diagonal elements of the matrix {1,2,3,4} will be equal to 5. Example 1 2 3 4 Sum = 1+4 = 5
asked Dec 22, 2020 Mashhoodch 10.8k points
1 vote
1 answer 279 views
279 views
Problem : I am new to C programming. I have recently started working on exercise. Please find below my code for your reference: int mydistinct(int a[], int n) { int i, k, j, count=0; int max = a[0]; for (i = 1; i < n; i++) { if (max < a[i]) { ... . In this case my code will only try to count 8,7,6,5 and it will return only four unique numbers. So I am looking for proper guidance in C programming.
asked May 19, 2020 Martin K 6.6k points
0 votes
1 answer 48 views
48 views
Problem: Hi there! I am a new learner. I want to know how I can find the position of an element given by the user in an array. I tried some programs but could get the logic. The element, whose position is to be found, will be taken as input from the ... has to choose any element of that array. Can anybody here help me understand the logic of the program? Example program will be a plus. Thanks!
asked Jan 2 Code Learner 7k points
0 votes
1 answer 20 views
20 views
Problem Hey, I have a problem in finding the sum of squared errors and R^2 after glm My formula is glm(formula = total ~ yr * mnth, data = daytots) mnth and yr are factors and total is numerical I want to find the sample sum of square error and R^2. but i think glm() function is not able to return both of them. Can someone help me to know how to find them?
asked Jan 15 chris jordan 2.4k points
0 votes
1 answer 17 views
17 views
Problem: How do we find the sum of integer elements in an single dimensional array?
asked Oct 13, 2020 Code Learner 7k points
0 votes
1 answer 16 views
16 views
Problem: Is there possibility of sum of ArrayList without looping List tt = new ArrayList(); tt.add(1); tt.add(2); tt.add(3);
asked Nov 4, 2020 Mashhoodch 10.8k points
0 votes
1 answer 6 views
6 views
Problem: I have a list of numbers, e.g. numbers = [1, 2, 3, 7, 7, 9, 10] As you can see, numbers may appear more than once in this list. I need to get all combinations of these numbers that have a given sum, e.g. 10. The items in the combinations may ... length restrictions for the combinations, [10] is as valid as [1, 2, 7]. How can I create a list of all combinations meeting the criteria above?
asked Feb 22 Ali 6.5k points