So for this problem, we have two variables one for a positive number and another for a negative number as I said above we compare the number with zero or if the number is greater than zero we will increase the positive variable by one for a negative increase negative variable by one. Before solving follow
Read: C Program To Check Number Is Positive Or Negative
C++ Program to Count Positive and Negative Numbers Using While Loop
#include <iostream>
using namespace std;
/*C++ program to count positive and negative numbers using while loop*/
int main()
{
int a[100], i, n, zero = 0, pos = 0, neg = 0;
cout << "Enter The Size of An Array :\n";
cin >> n;
cout << "Enter The Element :\n";
for (i = 0; i < n; i++)
{
cin >> a[i];
}
cout << "Elment in Array is Given Below\n";
i = 0;
while (i < n;)
{
if (a[i] > 0)
pos++;
else if (a[i] < 0)
neg++;
else
zero++;
i++;
}
cout << "\nPositive No. is = " << pos;
cout << "\nNegative No. is = " << neg;
cout << "\nTotal Zero in array is = " << zero;
return 0;
}
The Output of Count Positive and Negative Numbers Using While Loop
Similar to Positive and Negative Numbers
- C++ Program to Display the Grade of a Student Using Switch Case
- C++ Program to Convert Celsius to Fahrenheit And Vice Versa
- C++ Program to Perform Arithmetic Operations Using Switch Case
- C++ Program to Find Area of a Triangle/Square/Circle/ Rectangle Using Switch Statement
- C++ Program to Print Table of Any Number Using For Loop
0 Comments: