Write a program that reads in ten whole numbers and that output the sum of all the numbers greater than zero, the sum of all the numbers less than zero (which will be a negative number or zero), and the sum of all the numbers, whether positive, negative, or zero. The user enters the ten numbers just once each and the user can enter them in any order. Your program should not ask the user to enter the positive numbers and the negative numbers separately.
Assume the user will type integer numbers.
this is what i got but it wont run saying there is an error
#include<iostream>;
using namespace std;
int main()
{
int count=0;
int num;
int positive=0;
int negative=0;
do
{
cout<<"Enter 10 whole numbers"<<endl;
cout<<num;
if (num<=0)
{
negative= negative+num;
}
else
{
positive= positive+num;
}
count++;
}
while (count<10);
cout<<" Sum of postive number= "<< positive<<endl;
cout<<" Sum of negative number= "<<negative<<endl;
cout<<" Sum of all numbers= " <<positive+ negative<<endl;
}
I think line 10 cout << num; You meant cin >> num; to read[1] in a number. Not sure what error you are getting though could you specify please? Also could you please use code tags
[code][/code]
around your code. Also you can use += to replace x = x + .. They are compound operators[2] basically you could replace your negative/positive with negative += num; and same with positive.
well when i run my program it repeats the question"Enter 10 number" 10 times..
and this shows up
warning C4067: unexpected tokens following preprocessor directive - expected a newline
That's not an error message it's a warning message just letting you know that the semi-colon on line 1 is not needed. simply #include <iostream> will work.
I fixed all that but still the "Enter the 10 number" is repeating 10 times
#include<iostream>
using namespace std;
int main()
{
int count=0;
int num;
int positive=0;
int negative=0;
do
{
cout<<"Enter 10 whole numbers"<<endl;
cin>>num;
if (num<=0)
{
negative= negative+num;
}
else
{
positive= positive+num;
}
count++;
}
while (count < 10);
cout<<" Sum of postive number= "<< positive<<endl;
cout<<" Sum of negative number= "<<negative<<endl;
cout<<" Sum of all numbers= " <<(positive+ negative)<<endl;
}