Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
Code to count Positive, Negative and zero in an array of C

C++ Code to count Positive, Negative and zero in an array

Posted on November 4, 2020November 4, 2020

Table of Contents

  • C++ Code to count Positive, Negative and zero in an array
    • Program to count Positive, Negative and zero in an array
      • Count Positive, Negative and zero in an array using for loop
      • Count Positive, Negative and zero in an array using while loop
      • Count Positive, Negative and zero in an array using do-while loop
      • Count Positive, Negative and zero in an array using function

C++ Code to count Positive, Negative and zero in an array

In this article, we will discuss the concept of C++ Code to count Positive, Negative and zero in an array

In this program, we are going to learn how to count Positive numbers, Negative numbers and zeros in an array using several ways in C++ language.

This is done using for loop , while loop , do-while loop and function in C++ language

Program to count Positive, Negative and zero in an array

Count Positive, Negative and zero in an array using for loop

In this program, we will count Positive numers, Negative numbers and zeros in an array using a for loop in C++ language

Program 1

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int i,size,countPos=0, countNeg=0, countZero=0,arr[100];
    cout<<"Enter the array size\n";
    cin>>size;
    cout<<"Enter "<<size<<" elements\n";
    for(i=0; i<size; i++){
     cin>>arr[i];
    }
    for(i=0; i<size; i++){
     if(arr[i]<0){
        countNeg++;
     }
     else if(arr[i]>0){
        countPos++;
     }
     else{
        countZero++;
     }
    }
    cout<<"Positive numbers are: "<<countPos<<endl;
    cout<<"Negative numbers are:"<<countNeg<<endl;
    cout<<"zeros are "<<countZero;
    getch();
    return 0;
}


When the above code is executed it produces the following result

Enter the array size
11
Enter 11 elements
56
0
-67
49
09
0
-54
98
-34
98
-45

Positive numbers are:5
Negative numbers are: 4
zeros are: 2

 

Count Positive, Negative and zero in an array using while loop

In this program, we will count Positive numbers, Negative numbers and zeros in an array using a while loop in C++ language

Program 2

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int i,size,countPos=0, countNeg=0, countZero=0,arr[100];
    cout<<"Enter the array size\n";
    cin>>size;
    cout<<"Enter "<<size<<" elements\n";
    i=0;
    while(i<size){
     cin>>arr[i];
      i++;
    }
    i=0;
    while(i<size){
     if(arr[i]<0){
        countNeg++;
     }
     else if(arr[i]>0){
        countPos++;
     }
     else{
        countZero++;
     }
      i++;
    }
    cout<<"Positive numbers are: "<<countPos<<endl;
    cout<<"Negative numbers are: "<<countNeg<<endl;
    cout<<"zeros are "<<countZero;
    getch();
    return 0;
}

When the above code is executed it produces the following result

Enter the array size
10
Enter 10 elements

345
0
-562
678
-543
678
0
456
-542
874

Positive numbers are:5
Negative numbers are: 3
zeros are: 2

 

Count Positive, Negative and zero in an array using do-while loop

In this program, we will count Positive numbers, Negative numbers and zeros in an array a do-while loop in C++ language

Program 3

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int i,size,countPos=0, countNeg=0, countZero=0,arr[100];
    cout<<"Enter the array size\n";
    cin>>size;
    cout<<"Enter "<<size<<" elements\n";
    i=0;
   do{
     cin>>arr[i];
      i++;
    } while(i<size);
    i=0;
    do{
     if(arr[i]<0){
        countNeg++;
     }
     else if(arr[i]>0){
        countPos++;
     }
     else{
        countZero++;
     }
      i++;
    }while(i<size);
    cout<<"Positive numbers are: "<<countPos<<endl;
    cout<<"Negative numbers are: "<<countNeg<<endl;
    cout<<"zeros are "<<countZero;
    getch();
    return 0;
}




When the above code is executed it produces the following result

Enter the array size
8
Enter 8 elements

45
0
-54
34
67
-34
89
76

Positive numbers are:5
Negative numbers are: 2
zeros are: 1

 

 

Count Positive, Negative and zero in an array using function

In this program, we will count Positive numbers, Negative numbers and zeros in an array using function in C++ language

Program 4

#include <iostream>
#include <conio.h>
using namespace std;

int countPositive(int arr[],int size);
int countNegative(int arr[],int size);
int countZero(int arr[],int size);
int main()
{
    int size,i,arr[10];
    int posCount=0,negCount=0,zeroCount=0;
    cout<<"Please enter the size of an array \n";
    cin>>size;
      cout<<"\nPlease enter the array elements \n";
    for(i=0; i<size; i++){
        cin>>arr[i];

    }
    cout<<"\nTotal positive numbers in this array="<<countPositive(arr,size);
    cout<<"\nTotal Negative numbers in this array="<<countNegative(arr,size);
    cout<<"\n\nTotal zeros in this array= "<<countZero(arr,size);
    getch();
    return 0;
}

int countPositive(int arr[],int size){
int i, posCount=0;
cout<<"\nList of positive number in this array: \n";
 for(i=0; i<size; i++){
        if(arr[i]>0){
          cout<<arr[i];
          cout<<"\n";
            posCount++;
        }
    }
    return posCount;

}

int countNegative(int arr[],int size){
int i, negCount=0;
cout<<"\n\nList of negative number in this array: \n"<<endl;
 for(i=0; i<size; i++){
        if(arr[i]<0){
            cout<<arr[i];
            cout<<"\n";
            negCount++;
        }
    }
    return negCount;

}

int countZero(int arr[],int size){
int i, zeroCount=0;

 for(i=0; i<size; i++){
        if(arr[i]==0){
            zeroCount++;
        }
    }
    return zeroCount;

}

 

When the above code is executed it produces the following result

C++ Code to count Positive, Negative and zero in an array
Find positive using function

Suggested post

Operator in C++ language

for loop in C++ language

while loop in C++ language

do-while loop in C++ language

 

Similar post

Program to count Positive, Negative and zero in an array of C++

Program to count Positive, Negative and zero in an array of Python

Program to count Positive, Negative and zero in an array of Java

Program to count Positive, Negative and zero in an array of C

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes