008-005-009
Array Printer Program
Easy
Problem Description
Array Sum Calculator
In this problem, you will create a program that reads array elements from standard input, passes them to a sumArray() method, and prints the sum of all elements.
Learning Objective: Understand how to pass reference types (arrays) as method arguments
Overview
Read the array size and elements using Scanner, pass the array to a method, compute the total sum, and print it. Learn the basics of reference type arguments.
Specifications
Define a
sumArray()method:- Receives an int array as an argument
- Computes and returns the sum of all array elements (return type: int)
In Main class:
- Line 1: Read N (the number of elements)
- Line 2: Read N space-separated integers into an array
- Call
sumArray()passing the array and print the result
Input/Output Example
Input:
3
10 20 30
Output:
60
Input:
5
1 2 3 4 5
Output:
15
Hints
- Create a Scanner with
Scanner scanner = new Scanner(System.in); - Use
scanner.nextInt()to read one integer at a time - Specify array as method parameter like
int[] numbers - Use a for loop to accumulate each element
- Initialize a sum variable to 0 before the loop
Ready to Try Running Code?
Log in to access the code editor and execute your solutions for this problem.
Don't have an account?
Sign Up