SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
C++ Basics
Exercise:
Programming Projects
Question:10 | ISBN:9780321531346 | Edition: 7

Question

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

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

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      

 

0 0

Discussions

Post the discussion to improve the above solution.