Skip to content

Commit 7517bf8

Browse files
Merge pull request kishanrajput23#177 from SOMAN-SABEEL/main
Java
2 parents 440c935 + b5c87f2 commit 7517bf8

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

sum.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
1-
public class SumThreeNumbers {
1+
import java.util.Scanner;
2+
3+
public class SumNumbers {
24
public static void main(String[] args) {
3-
int num1 = 5;
4-
int num2 = 10;
5-
int num3 = 15;
6-
7-
int sum = num1 + num2 + num3;
8-
9-
System.out.println("The sum of " + num1 + ", " + num2 + ", and " + num3 + " is: " + sum);
5+
Scanner scanner = new Scanner(System.in);
6+
int sum = 0;
7+
8+
System.out.println("Enter numbers (enter 0 to calculate the sum):");
9+
10+
while (true) {
11+
int number = scanner.nextInt();
12+
if (number == 0) {
13+
break; // Exit the loop if the user enters 0
14+
}
15+
sum += number;
16+
}
17+
18+
System.out.println("The sum of the numbers is: " + sum);
19+
20+
scanner.close(); // Don't forget to close the scanner
1021
}
1122
}

0 commit comments

Comments
 (0)