# count the number of positive and negative using while loop.

Page 1 of 1

## 1 Replies - 222 Views - Last Post: 21 March 2019 - 08:20 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=415432&amp;s=6632e5e373432bf4ea03cdfde563a080&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 fhaz

Reputation: 0
• Posts: 1
• Joined: 21-March 19

# count the number of positive and negative using while loop.

Posted 21 March 2019 - 07:11 PM

```#include <iostream>
#include <string>

using namespace std;
int main()
{

// lets declare some variable first.
int positiveSum =0;  //this will hold sum of positive nums
int negativeSum =0; // this will hold sum of negative nums
int totalSum =0; // this will hold sum of all the nums
int number; // user input for number

cout << " Enter a number: ";
cin >> number; // now check if number is positive or negative
while (totalSum >= -100.0 && totalSum <= 100.0)  // loop from 1 to 10 times
{	totalSum = totalSum + number;
if (number >=0)
{ int count = count + 1;
positiveSum += number;    // adds this number to positiveSum
}
else if (number < 0)
{	int count = count + 1;

negativeSum += number;   // adds this number to negativeSum
}

cout << " Enter a number: ";
cin >> number;
}

// So finally add the positiveSum and negativeSum to get the totalSum
totalSum = positiveSum + negativeSum;

cout << endl;
cout << " Total of Positive numbers is: " << positiveSum << endl;
cout << " Total of Negative numbers is: " << negativeSum << endl;
cout << " Total of all numbers is: " << totalSum << endl;

return 0;
}
```

This post has been edited by #define: 21 March 2019 - 08:13 PM
Reason for edit:: add code tags

Is This A Good Question/Topic? 0

## Replies To: count the number of positive and negative using while loop.

### #2 #define

• Duke of Err

Reputation: 1858
• Posts: 6,690
• Joined: 19-February 09

## Re: count the number of positive and negative using while loop.

Posted 21 March 2019 - 08:20 PM

Welcome to Dream in Code.

If you wish to use a count for the loop, you would need create the variable before the while loop. You could search for instances of counting with a while loop.