FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
My-Java-Journey/EvenSum.java at main · aayanshanand/My-Java-Journey · GitHub
aayanshanand
/
My-Java-Journey
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
4
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
My-Java-Journey
/
EvenSum.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
26 lines (21 loc) · 1009 Bytes
Breadcrumbs
My-Java-Journey
/
EvenSum.java
Copy path
File metadata and controls
26 lines (21 loc) · 1009 Bytes
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
import
java
.
util
.
Scanner
;
// Import Scanner class for user input
public
class
EvenSum
{
// Function to calculate the sum of even numbers from 1 to n
public
static
int
sumOfEvenNum
(
int
n
) {
int
sum
=
0
;
// Variable to store the sum of even numbers
// Loop through even numbers from 2 to n
for
(
int
i
=
2
;
i
<=
n
;
i
+=
2
) {
sum
+=
i
;
// Add current even number to sum
}
return
sum
;
// Return the final sum
}
public
static
void
main
(
String
[]
args
) {
// Create a Scanner object to take user input
try
(
Scanner
scanner
=
new
Scanner
(
System
.
in
)) {
System
.
out
.
print
(
"Enter the value of n: "
);
// Prompt user for input
int
n
=
scanner
.
nextInt
();
// Read input from user
// Print the result using the sumOfEvenNum function
System
.
out
.
println
(
"Sum of even numbers from 1 to "
+
n
+
" is: "
+
sumOfEvenNum
(
n
));
}
}
}
Back
|
FazBrowse Home
|
New Git URL