KnowledgeBoat Logo

Computer Applications

Write a program to input any 50 numbers (including positive and negative). Perform the following tasks:

(a) Count the positive numbers

(b) Count the negative numbers

(c) Sum of positive numbers

(d) Sum of negative numbers

Java

Java Iterative Stmts

ICSE

110 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, nSum = 0, nCount = 0;
        System.out.println("Enter 50 numbers");
        
        for (int i = 1; i <= 50; i++) {
            int n = in.nextInt();
            if (n >= 0) {
                pSum += n;
                pCount++;
            }
            else {
                nSum += n;
                nCount++;
            }
        }
        
        System.out.println("Positive Count = " + pCount);
        System.out.println("Positive Sum = " + pSum);
        System.out.println("Negative Count = " + nCount);
        System.out.println("Negative Sum = " + nSum);
    }
}

Output

BlueJ output of Write a program to input any 50 numbers (including positive and negative). Perform the following tasks: (a) Count the positive numbers (b) Count the negative numbers (c) Sum of positive numbers (d) Sum of negative numbersBlueJ output of Write a program to input any 50 numbers (including positive and negative). Perform the following tasks: (a) Count the positive numbers (b) Count the negative numbers (c) Sum of positive numbers (d) Sum of negative numbers

Answered By

43 Likes


Related Questions