FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
java-coding-ninjas/recursion1/ArraySum.java at master · avinashbest/java-coding-ninjas · GitHub
avinashbest
/
java-coding-ninjas
Public
Notifications
You must be signed in to change notification settings
Fork
11
Star
18
Code
Issues
0
Pull requests
2
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-coding-ninjas
/
recursion1
/
ArraySum.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
48 lines (44 loc) · 1.15 KB
Breadcrumbs
java-coding-ninjas
/
recursion1
/
ArraySum.java
Copy path
File metadata and controls
48 lines (44 loc) · 1.15 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
package
recursion1
;
import
java
.
util
.
Scanner
;
/*Given an array of length N, you need to find and return the sum of all elements of the array.
Do this recursively.
Input Format :
Line 1 : An Integer N i.e. size of array
Line 2 : N integers which are elements of the array, separated by spaces
Output Format :
Sum
Constraints :
1 <= N <= 10^3
Sample Input 1 :
3
9 8 9
Sample Output 1 :
26
Sample Input 2 :
3
4 2 1
Sample Output 2 :
7 */
public
class
ArraySum
{
public
static
int
arraySum
(
int
[]
arr
,
int
startIndex
) {
if
(
arr
.
length
==
startIndex
) {
return
0
;
}
return
arr
[
startIndex
] +
arraySum
(
arr
,
startIndex
+
1
);
}
public
static
int
[]
takeInput
() {
Scanner
scan
=
new
Scanner
(
System
.
in
);
System
.
out
.
print
(
"Size of the Array? "
);
int
size
=
scan
.
nextInt
();
int
[]
arr
=
new
int
[
size
];
System
.
out
.
println
(
"Enter elements of the Array: "
);
for
(
int
i
=
0
;
i
<
size
;
i
++) {
arr
[
i
] =
scan
.
nextInt
();
}
return
arr
;
}
public
static
void
main
(
String
[]
args
) {
int
[]
arr
=
takeInput
();
System
.
out
.
println
(
arraySum
(
arr
,
0
));
}
}
Back
|
FazBrowse Home
|
New Git URL