006-005-005
Enhanced For Loop: Array Sum
Easy
Problem Description
Enhanced for loop: array Sum
In this problem, you will create a program that reads an integer array from standard input, iterates over all elements using an enhanced for loop, accumulates the sum, and displays the result to standard output.
Learning Objective: Understand how to process array elements sequentially using enhanced for loop
Overview
Read an integer array from standard input. Calculate the sum of all elements using an enhanced for loop and display the result.
Input Format
n
a1
a2
...
an
- Line 1: number of elements n
- Subsequent lines: n integers, one per line
Specifications
- Use
Scannerto read the array from standard input - Use enhanced for loop to retrieve each element
- Calculate sum
- Output in format "Sum: total"
Output Format
Sum: total
Sample Input/Output
Input:
5
10
20
30
40
50
Output:
Sum: 150
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