-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumOfNumbers.java
More file actions
23 lines (19 loc) · 810 Bytes
/
Copy pathSumOfNumbers.java
File metadata and controls
23 lines (19 loc) · 810 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Take integer inputs till the user enters 0 and print the sum of all numbers
import java.util.Scanner;
public class SumOfNumbers {
public static void main(String[] args) {
// Scanner object to take input from standard input stream
Scanner scanner = new Scanner(System.in);
int sum = 0; // Variable to store sum of all numbers
int input; // Variable to store input numbers
// Logic for repetitive input and sum calculation
// Use do while loop to start and stop when user enter 0
System.out.println("Keep entering number, to stop enter 0");
do {
input = scanner.nextInt();
sum += input;
} while (input != 0);
// Printing result
System.out.println("Sum of all numbers is: " + sum);
}
}