156k views
3 votes
Consider the array called myArray declared below that contains positive and negative integers. Write a loop statement to count and display the number of positive integers in the array. That is, your loop should display the message "Number of positive integers is 5 ". int myArray[] = { -1, 3, -9, 9, 33, -4, -5, 10, 4, -23];

1 Answer

3 votes

Answer:

#include <iostream>

using namespace std;

int main() {

int arr[100],n,count=0;

cin>>n;//taking input of size.

for(int i=0;i<n;i++)

cin>>arr[i];//taking input of array elements.

for(int i=0;i<n;i++)

{

if(arr[i]>0)//counting positive integers.

{

count++;

}

}

cout<<"Number of positive integers is "<<count<<endl;//printing the message..

return 0;

}

9

1 8 -8 -6 2 4 6 7 -2

Output:-

Number of positive integers is 6

Step-by-step explanation:

Above written is the code for counting positive integers in an array.I have used a count variable and initialized it with 0.On iterating over the array if the element is greater than 0 means positive then increasing the count by 1.At last printing the message.

User Renato Probst
by
4.7k points