9617. Number of positive
Find the number of positive integers in the
given array.
Input. The first line contains the number of integers n
(1 ≤ n ≤ 100) in the array. The second line contains n
integers, each with an absolute value not exceeding 100.
Output. Print the number of positive integers in the array.
Sample
input |
Sample
output |
5 -5 6 8 -3 0 |
2 |
loops
Algorithm analysis
Declare a variable c to count the
number of positive numbers in the array. Initially, assign it the value c = 0. Then, iterate through all the input numbers and
increment c by 1 for each positive number.
Algorithm implementation
Declare an
array.
int m[101];
Read the
input data.
scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%d", &m[i]);
Count the
number of positive integers in the variable c.
c = 0;
for (i = 0; i < n; i++)
if (m[i] > 0) c++;
Print the
answer.
printf("%d\n", c);
Java implementation
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner con = new Scanner(System.in);
int n = con.nextInt();
int c = 0;
for(int i = 0; i < n; i++)
{
int val = con.nextInt();
if (val >
0) c++;
}
System.out.println(c);
con.close();
}
}
Python implementation
Read the
input data.
n = int(input())
lst = list(map(int, input().split()))
Count the
number of positive integers in the variable c.
c = 0
for i in range(n):
if lst[i] > 0: c += 1
Print the
answer.
print(c)
Python implementation
– list
Read the
input data.
n = int(input())
lst = list(map(int, input().split()))
Create a list p that contains only positive integers.
p = [x for x in
lst if x > 0]
The length of the list p equals the count of
positive integers.
res = len(p)
Print the
answer.
print(res)