- Home
- Chapter 1 - Object Oriented Programming Concepts
- Object Oriented Programming Concepts
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 2 - Introduction to Java
- Introduction to Java
- Multiple Choice Questions
- Assignment Questions
- Chapter 3 - Values and Data Types
- Values and Data Types
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 4 - Operators in Java
- Operators in Java
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 5 - User-Defined Methods
- User-Defined Methods
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 6 - Input in Java
- Input in Java
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 7 - Mathematical Library Methods
- Mathematical Library Methods
- Multiple Choice Questions
- Assignment Questions
- Chapter 8 - Conditional Constructs in Java
- Conditional Constructs in Java
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 9 - Iterative Constructs in Java
- Iterative Constructs in Java
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions and Programs
- Chapter 10 - Nested for loops
- Nested for loops
- Assignment Questions and Programs
- Chapter 11 - Constructors
- Constructors
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 12 - Library Classes
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 13 - Encapsulation and Inheritance
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 14 - Arrays
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 15 - String Handling
- Library Classes
- Multiple Choice Questions
- Assignment Questions
Iterative Constructs in Java
Chapter 9
Iterative Constructs in Java
Class 10 - Logix Kips ICSE Computer Applications with BlueJ
![]() Share with a Friend |
Java Program: Sum of Negative, Positive Even, and Positive Odd Numbers
32. Write a program to print the sum of negative numbers, sum of positive even numbers and sum of positive odd numbers from a list of n numbers entered by the user. The program terminates when the user enters a zero.
Program Title: Sum of Negative, Positive Even, and Positive Odd Numbers
import java.util.Scanner;
public class SumNumbers {
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 (0 to terminate):");
while (true) {
int num = sc.nextInt();
if (num == 0) {
break; // terminate when 0 is entered
}
if (num < 0) {
sumNegative += num;
} else if (num > 0 && num % 2 == 0) {
sumPositiveEven += num;
} else if (num > 0 && num % 2 != 0) {
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);
sc.close();
}
}
Output
Sample Input / Output Enter numbers (0 to terminate): 5 -2 8 -7 3 0 Sum of negative numbers: -9 Sum of positive even numbers: 8 Sum of positive odd numbers: 8
How It Works
- Use a while loop that runs indefinitely.
- Read a number from the user. If it’s 0, break the loop.
- Check the number:
- If negative → add to sumNegative
- If positive and even → add to sumPositiveEven
- If positive and odd → add to sumPositiveOdd
- Display all sums at the end.
