Number of positive#9617

Number of positive (Editorial)

Declare a variable 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 by 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 = 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 = 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 that contains only positive integers.

p = [x for x in lst if x > 0]

The length of the list equals the count of positive integers.

res = len(p)

Print the answer.

print(res)