004-010-007
Iterating Over Arrays
Easy
Problem Description
Iterating Over Arrays
In this problem, you will create a program that reads an integer array from standard input and processes all elements using both a standard for loop and an enhanced for loop (for-each), computing the sum and doubling each element.
Learning Objective: Understand how to process all elements of an array using a standard for loop and an enhanced for loop (for-each)
Overview
Read an integer array from standard input and process all elements using two approaches (standard for loop and enhanced for loop), outputting the sum and double of each element.
Input Format
N
a1 a2 ... aN
- Line 1: number of elements
N - Line 2:
Nspace-separated integers
Specifications
- Use Scanner to read N and the integer array
- Use a standard for loop to calculate and print the sum of the array
- Use an enhanced for loop (for-each) to print double of each element, one per line
Output Format
Sum: <total>
<element1 * 2>
<element2 * 2>
...
Example
Input:
5
10 20 30 40 50
Output:
Sum: 150
20
40
60
80
100
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