1 Replies - 222 Views - Last Post: 21 March 2019 - 08:20 PM Rate Topic: -----

#1 fhaz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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   User is offline

  • Duke of Err
  • member icon

Reputation: 1858
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1