codescracker


c++

C++ Program to Count Positive, Zero and Negative Numbers



« Previous Program Next Program »


Count Positive, Negative and Zero in C++

To count the number of positive number, negative number, and zero from the given set of numbers entered by the user in C++ programming, you have to ask to the user to enter some set of numbers (10 numbers here). Now to find occurrence of positive, negative and zero from the given set of numbers, just check all the numbers using for loop whether the number is 0, less than zero or greater than 0 to count the occurrence of positive, negative and zero as shown here in the following program.

C++ Programming Code to Count Occurrence of Positive, Zero and Negative Numbers

Following C++ program ask to the user to enter 10 numbers to check for number of positive number, negative number and zero occurrence, then display the result on the screen:

/* C++ Program - Count Occurrence of Numbers */
		
#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	int countp=0, countn=0, countz=0, arr[10], i;
	cout<<"Enter 10 numbers : ";
	for(i=0; i<10; i++)
	{
		cin>>arr[i];
	}
	for(i=0; i<10; i++)
	{
		if(arr[i]<0)
		{
			countn++;
		}
		else if(arr[i]==0)
		{
			countz++;
		}
		else
		{
			countp++;
		}
	}
	cout<<"Positive Numbers = "<<countp<<"\n";
	cout<<"Negative Numbers = "<<countn<<"\n";
	cout<<"Zero = "<<countz<<"\n";
	getch();
}

When the above C++ program is compile and executed, it will produce the following result:

C++ program count positive negative zero

Same Program in Other Language

You may like the same program in other programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test