How Do I find the sum of positive and negative numbers in a group of 10 different numbers?

EDIT: I no longer need help. Thanks to all of you who gave their time to help me! ;)

Okay, so I have just started the C++ language, and there are A LOT of things that I do not know... For instance, I am trying to write a program that lets the user input 10 different numbers (any number, whether it's + or -), then finds the sum of all of the positive numbers, outputs it, finds the sum of all the negative numbers, outputs it, and then finds the sum of all 10 numbers and outputs it. I do realize that I need to use different loops, but I am having some trouble figuring out which loops to use and how... I don't have any code to show because it isn't helpful :/
Last edited on
Can you loop over the array and print out the individual elements?
Okay, so you can use several things here. You can use arrays or vectors which can store a bunch of numbers. But I don't think you have learned it as you seem to have just started learning. So let me show you another trick that also works. Follow carefully.


What we'll do is ask user to input a number 10 times and as the user inputs, we will do the math each step. So after the user enter first number, we'll add that to 0. Then the user enters second number, we'll add that to the first number. Then the user enters, 3rd number and so we'll add that to the sum of first 2 numbers and so on.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

#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=0; // user input for number
for (int i = 1; i <=10; i++)  // loop from 1 to 10 times
{
    cout << " Enter a number: ";
    cin >> number;
    
    // now check if number is positive or negative
    if (number >=0)
    {
        positiveSum += number;    // adds this number to positiveSum
    }
    else if (number < 0)
   {
       negativeSum += number;   // adds this number to negativeSum
   }

} 

// 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;
}



Sample output:
 Enter a number: 23
 Enter a number: 45
 Enter a number: -69
 Enter a number: 24
 Enter a number: -14
 Enter a number: 63
 Enter a number: -41
 Enter a number: -74
 Enter a number: 12
 Enter a number: 00

 Total of Positive numbers is: 167
 Total of Negative numbers is: -198
 Total of all numbers is: -31


Hope this helps!
Last edited on
You're a life saver! Thank you sooo much! I had started a class in C++ late since I switched from another class, so I missed a lot of important stuff. I'll be sure not to just copy and paste the code though xD I'll go through and learn it and then write out my own code myself. Thanks again!
You're welcome :)
Topic archived. No new replies allowed.