KnowledgeBoat Logo

Computer Applications

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

i. Number of positive numbers

ii. Number of negative numbers

iii. Sum of positive numbers

Java

Java Iterative Stmts

11 Likes

Answer

import java.util.Scanner;

public class KboatIntegers
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        int pSum = 0, pCount = 0, nCount = 0;
        System.out.print("Enter the number of integers: ");
        int num = in.nextInt();
        
        System.out.println("Enter " + num + " integers: ");
        for (int i = 1; i <= num; i++) {
            int n = in.nextInt();
            if (n >= 0) {
                pSum += n;
                pCount++;
            }
            else 
                nCount++;
        }
        
        System.out.println("Positive numbers = " + pCount);
        System.out.println("Negative numbers = " + nCount);
        System.out.println("Sum of positive numbers  = " + pSum);
    }
}

Output

BlueJ output of Write a program to accept n number of input integers and find out: i. Number of positive numbers ii. Number of negative numbers iii. Sum of positive numbers

Answered By

4 Likes


Related Questions