ICSE Class 9 Computer Applications
Question 10 of 29
Iterative Constructs in Java — Question 13
Back to all questions 13
Question Question 9
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
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
