Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

What am I doing wrong that the program returns zero as the sum? This is a progra

ID: 3598467 • Letter: W

Question

What am I doing wrong that the program returns zero as the sum? This is a program in java. User inputs 10 numbers and then there is a method call to sum those numbers.

package summ;

import java.util.*;

import java.util.Scanner;

public class summ

{

  

   public static void main(String[] args)

   {

      

       int [] num = new int [10];

       Scanner input = new Scanner(System.in);

       System.out.println("Input ten integers:");

      

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

       num[i]= input.nextInt();

      

       sumNum(num);

      

   }

      

       public static int sumNum (int[] list)

       {

           int sum=0;

          System.out.println("The sum of the array is " + sum);

      

   for (int i=0; i<= list.length-1; i++) {

sum += list[i];

}

return sum;

}

      

}

Explanation / Answer

Hi

I have fixed the issue and highlighted the code changes below.

summ.java

import java.util.*;

import java.util.Scanner;

public class summ {

public static void main(String[] args) {

int[] num = new int[10];

Scanner input = new Scanner(System.in);

System.out.println("Input ten integers:");

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

num[i] = input.nextInt();

int sum =sumNum(num);

System.out.println("The sum of the array is " + sum);

}

public static int sumNum(int[] list) {

int sum = 0;

for (int i = 0; i <= list.length - 1; i++) {

sum += list[i];

}

return sum;

}

}

Output:

Input ten integers:
1 2 3 4 5 6 7 8 9 10
The sum of the array is 55

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote