Java program to count positive numbers present in an array | Array in Java | Ask The Code
Problem:
There are different kinds of numbers, but among them all, the most commonly used numbers in our daily lives are those positive ones. So, I only want you to count all the positive numbers from the 4 outputted values and print out the result. Go and search for the positive ones among these four!
Input Format: A line containing four numbers (may contain decimal places) separated by a space
Input Sample:
2 -4 3.6 1
Output Format:
A line containing an integer.
Output Sample:
3
Code:
import java.util.Scanner;
public class solution_Problem3{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int count = 0;
float arr[] = new float[4];
for (int i = 0; i < 4; i++) {
arr[i] = sc.nextFloat();
}
for (int i = 0; i < 4; i++) {
if (arr[i] > 0) {
count += 1;
}
}
System.out.println(count);
}
}
Comentarios