10. Write a program that reads in ten whole numbers and that outputs 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
Program Code:
//header file
#include <iostream>
using namespace std;
//Program begins from the main method
int main()
{
//Define variables
double inputNums;
double sumOfPositiveNums=0;
double sumOfNegativeNums=0;
double totalOfAllNums=0;
//Readsten whole numbers
cout<<" Enter any 10 numbers(whether positive, negative, or zero): "<<endl;
for(int i=0;i<10;i++)
{
cin>>inputNums;
totalOfAllNums +=inputNums ;
if(inputNums>0)//validate negative
sumOfPositiveNums += inputNums;
else
sumOfNegativeNums += inputNums;
}
//Print sum of all positive numbers
cout<<"The sum of all the numbers greater than zero(all positive numbers): "
<<sumOfPositiveNums<<endl;
//Print sum of all negative numbers
cout<<"the sum of all the numbers less than zero (which will be a negative number or zero): "
<<sumOfNegativeNums<<endl;
//Print total sum of numbers
cout<<"The sum of all the numbers : "<<totalOfAllNums<<endl;
}
Output of the program code:
Enter any 10 numbers(whether positive, negative, or zero):
10
9
5
6
-3
-2
4
2
0
8
The sum of all the numbers greater than zero(all positive numbers): 44
the sum of all the numbers less than zero (which will be a negative number or zero): -5
The sum of all the numbers : 39