Table of Contents
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
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
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
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
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
Suggested post
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
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.