Solutions for Class 10 ICSE Logix Kips Computer Applications with BlueJ Java | IT Developer <?php echo $page_title; ?>
IT Developer

Conditional Constructs in Java

Chapter 8

Conditional Constructs in Java

Class 10 - Logix Kips ICSE Computer Applications with BlueJ


Share with a Friend

Java Program: Analysis of Positive and Negative Integers


11. Write a program to input n number of integers and find out:

    i. Number of positive integers

   ii. Number of negative integers

  iii. Sum of positive numbers

   iv. Product of negative numbers

   v. Average of positive numbers

 

import java.util.Scanner;

 

public class IntegerAnalysis {

    public static void main(String[] args) {

 

        Scanner sc = new Scanner(System.in);

 

        int n;

        int num;

        int countPos = 0, countNeg = 0;

        int sumPos = 0;

        int productNeg = 1;

 

        System.out.print("Enter number of integers: ");

        n = sc.nextInt();

 

        for (int i = 1; i <= n; i++) {

            System.out.print("Enter integer " + i + ": ");

            num = sc.nextInt();

 

            if (num > 0) {

                countPos++;

                sumPos += num;

            } else if (num < 0) {

                countNeg++;

                productNeg *= num;

            }

        }

 

        System.out.println("\nNumber of positive integers: " + countPos);

        System.out.println("Number of negative integers: " + countNeg);

        System.out.println("Sum of positive integers: " + sumPos);

 

        if (countNeg > 0)

            System.out.println("Product of negative integers: " + productNeg);

        else

            System.out.println("Product of negative integers: Not applicable");

 

        if (countPos > 0)

            System.out.println("Average of positive integers: " + (sumPos / (double) countPos));

        else

            System.out.println("Average of positive integers: Not applicable");

 

        sc.close();

    }

}

Output

Sample Input / Output

Enter number of integers: 5
Enter integer 1: 4
Enter integer 2: -2
Enter integer 3: 6
Enter integer 4: -3
Enter integer 5: 5

Number of positive integers: 3
Number of negative integers: 2
Sum of positive integers: 15
Product of negative integers: 6
Average of positive integers: 5.0

📝 Explanation

  • countPos → counts positive numbers
  • countNeg → counts negative numbers
  • sumPos → accumulates sum of positive numbers
  • productNeg → multiplies negative numbers
  • Average of positives =
  • sum of positive numbers / number of positive numbers
  • Zero is ignored (neither positive nor negative)