FOR FREE CONTENT

Find how many negative numbers are present in the array

Back to Programming

Description

The elements of the array are taken as inputs from the user. This program finds how many negative numbers are present in the array. If an element is less than 0 means the element is the negative element and a variable is taken to count the number of negative elements.

 

Algorithm

INPUT: Array elements

OUTPUT: the number of negative elements of the array

PROCESS:

Step 1: [taking the input]

                Read n [number of elements]

                For i=0 to n-1 repeat

                                Read a[i]

                [end of ‘for’ loop]

Step 2: [finding the number of negative elements]

                Set count<-0

                For i=0 to n-1 repeat

                                If a[i]<0 then

                                                Set count<-count+1

                                [End of ‘if’]

                [End of ‘for’ loop]

                Print count

Step 3: Stop.

Code

Complexity:

The time complexity to take the number of elements is O(n) and the time complexity to count the negative numbers is also O(n).