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
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);
}
}
Variable Description Table
Program Explanation
Output
Related Questions
Write a program to enter any 50 numbers and check whether they are divisible by 5 or not. If divisible then perform the following tasks:
(a) Display all the numbers ending with the digit 5.
(b) Count those numbers ending with 0 (zero).
Write the program in Java to display the first ten terms of the following series:
2, 5, 10, 17,
Write a program to calculate the sum of all odd numbers and even numbers between a range of numbers from m to n (both inclusive) where m < n. Input m and n (where m < n).
Write the program in Java to display the first ten terms of the following series:
24, 99, 224, 399,