-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpositvenegativesum.java
More file actions
33 lines (26 loc) · 971 Bytes
/
positvenegativesum.java
File metadata and controls
33 lines (26 loc) · 971 Bytes
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
import java.util.Scanner;
public class positvenegativesum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int sumNegative = 0;
int sumPositiveEven = 0;
int sumPositiveOdd = 0;
System.out.println("Enter numbers (terminate with 0):");
while (true) {
int num = sc.nextInt();
if (num == 0) {
break; // Exit the loop when 0 is entered
}
if (num < 0) {
sumNegative += num;
} else if (num % 2 == 0) {
sumPositiveEven += num;
} else {
sumPositiveOdd += num;
}
}
System.out.println("Sum of negative numbers: " + sumNegative);
System.out.println("Sum of positive even numbers: " + sumPositiveEven);
System.out.println("Sum of positive odd numbers: " + sumPositiveOdd);
}
}