FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java_Fundamentals/ReturnArraySum.java at main · devdeepak06/Java_Fundamentals · GitHub
devdeepak06
/
Java_Fundamentals
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Java_Fundamentals
/
ReturnArraySum.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
53 lines (49 loc) · 1.61 KB
Breadcrumbs
Java_Fundamentals
/
ReturnArraySum.java
Copy path
File metadata and controls
53 lines (49 loc) · 1.61 KB
Raw
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
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//Given an array/list(ARR) of length N, you need to find and return the sum of all the elements in the array/list.
// Input Format :
// The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
// First line of each test case or query contains an integer 'N' representing the size of the array/list.
// Second line contains 'N' single space separated integers representing the elements in the array/list.
// Output Format :
// For each test case, print the sum of the numbers in the array/list, in a separate line.
// Output for every test case will be printed in a separate line.
// Constraints :
// 1 <= t <= 10^2
// 0 <= N <= 10^6
// Time Limit: 1 sec
// Sample Input 1:
// 1
// 3
// 9 8 9
// Sample Output 1:
// 26
// Sample Input 2:
// 2
// 5
// 1 2 3 4 5
// 3
// 10 20 30
// Sample Output 2:
// 15
// 60
import
java
.
util
.
Scanner
;
public
class
ReturnArraySum
{
public
static
int
sum
(
int
[]
arr
) {
int
sum
=
0
;
for
(
int
i
=
0
;
i
<
arr
.
length
;
i
++) {
sum
+=
arr
[
i
];
}
return
sum
;
}
public
static
void
main
(
String
[]
args
) {
try
(
Scanner
sc
=
new
Scanner
(
System
.
in
)) {
System
.
out
.
println
(
"Enter the size of array: "
);
int
n
=
sc
.
nextInt
();
int
[]
arr
=
new
int
[
n
];
System
.
out
.
println
(
"Enter the elements of array: "
);
for
(
int
i
=
0
;
i
<
n
;
i
++) {
arr
[
i
] =
sc
.
nextInt
();
}
System
.
out
.
println
(
"The sum of elements of array is: "
+
sum
(
arr
));
}
}
}
Back
|
FazBrowse Home
|
New Git URL