Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
19 lines (18 loc) · 1.14 KB

File metadata and controls

19 lines (18 loc) · 1.14 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.util.Scanner;
class SumDemo{
public static void main(String args[]){
Scanner scanner = new Scanner(System.in); //Scanner class is used to take inputs.
int[] array = new int[10];
int sum = 0;
System.out.println("Enter the elements:");
for (int i=0; i<10; i++)
{
array[i] = scanner.nextInt(); // This method fills the array by taking input from the user || the array index ranges from(0,9) in the loop.
}
for( int num : array) { //initialises num value to first element of array and updates it to next element every time travells in the loop
//condition breaks when the num=last element in the array
sum = sum+num; //for every iteration it adds the num(element of array) to the sum.
}
System.out.println("Sum of array elements is:"+sum); //A general statement to print the text inside "" is printed as it is and the sum is a variable of which value is printed
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.