Question

Use C++ Write a program that first reads in how many whole numbers the user wants...

Use C++

Write a program that first reads in how many whole numbers the user wants to sum, then reads in that many whole numbers, and finally 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 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.

The result should look exactly like:

-bash-4.3$ ./sum
How many numbers will you enter?
6
Enter 6 whole numbers while I count down.
Entries left to go: 6
19
Entries left to go: 5
-4
Entries left to go: 4
0
Entries left to go: 3
-6
Entries left to go: 2
3
Entries left to go: 1
-4
The sum of 2 numbers greater than zero is 22.
The sum of 3 numbers less than zero is -14.
The sum of all 6 numbers is 8.
Goodbye.

If the user enters 0 or less in response to the first question, instead of executing the rest, the program simply must print "There is nothing to sum."

How many numbers will you enter?
0
There is nothing to sum.
Goodbye.

Homework Answers

Answer #1

Here is C++ version of the program

#include <iostream>
using namespace std;
int main()
{
   /*these variables will hold the results of +ve and -ve numbers. The sum of
   all numbers is nothing sumPositive+sumNegative */
   int sumPositive=0,sumNegative=0;
  
   int n,countP=0,countN=0,i,num; /*counters to keep track of +ve,-ve and all numbers*/
  
   cout<<"\nHow many numbers will you enter ?\n ";
   cin>>n;
   /*check if user enter 0 or less, then nothing to do*/
   if(n<=0)
   {  
       cout<<"\nThere is nothing to sum.";
      
   }
   else
   {
  
       cout<<"\nEnter "<<n <<"whole numbers while I count down.";
      
       /*read input from user*/
       for(i=n;i>0;i--)
       {
           cout<<"\nEntries left to go: "<<i<<" \n";
           cin>>num;
          
          
           /*check if number is greater than 0 , then add it to sumPositive , otherwise
           check if its less than 0, then add it to sumNegative and also increase corresponding
           counters*/
           if(num>0)
           {
          
               sumPositive+=num;
               countP++;
           }
           else if(num<0)
           {
          
               sumNegative+=num;
               countN++;
           }
           /*nothing to do for 0 itself*/              
       }
       int total=sumPositive+sumNegative;
       /*display the results*/
       cout<<"\nThe sum of "<<countP<<" numbers greater than zero is "<<sumPositive;
       cout<<"\nThe sum of "<<countN<<" numbers less than zero is "<<sumNegative;
       cout<<"\nThe sum of all "<<n<<" numbers is "<<total;
   }
   cout<<"\nGoodbye.";
   return 0;  
  
}

====================================

Here is the C program based on the question given. Also I have put images of 2 sample runs. If you find it correct please don't forget to rate the answer. Thanks

=======================================

#include <stdio.h>
int main()
{
   /*these variables will hold the results of +ve and -ve numbers. The sum of
   all numbers is nothing sumPositive+sumNegative */
   int sumPositive=0,sumNegative=0;
  
   int n,countP=0,countN=0,i,num; /*counters to keep track of +ve,-ve and all numbers*/
  
   printf("\nHow many numbers will you enter ?\n ");
   scanf("%d",&n);
   /*check if user enter 0 or less, then nothing to do*/
   if(n<=0)
   {  
       printf("\nThere is nothing to sum.");
      
   }
   else
   {
  
       printf("\nEnter %d whole numbers while I count down.",n);
      
       /*read input from user*/
       for(i=n;i>0;i--)
       {
           printf("\nEntries left to go: %d \n",i);
           scanf("%d",&num);
          
          
           /*check if number is greater than 0 , then add it to sumPositive , otherwise
           check if its less than 0, then add it to sumNegative and also increase corresponding
           counters*/
           if(num>0)
           {
          
               sumPositive+=num;
               countP++;
           }
           else if(num<0)
           {
          
               sumNegative+=num;
               countN++;
           }
           /*nothing to do for 0 itself*/              
       }
      
       /*display the results*/
       printf("\nThe sum of %d numbers greater than zero is %d",countP,sumPositive);
       printf("\nThe sum of %d numbers less than zero is %d",countN,sumNegative);
       printf("\nThe sum of all %d numbers is %d",n,(sumPositive+sumNegative));
   }
   printf("\nGoodbye.");
   return 0;  
  
}

=================================

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a complete C++ program asking the user to enter numbers one by one. User enters...
Write a complete C++ program asking the user to enter numbers one by one. User enters only whole numbers 0 and 10. Once the user enters 0, then the program should print sum of only odd numbers entered by user prior to 0. A valid scenario given below: Enter a number: 5 Enter a number: 3 Enter a number: 2 Enter a number: 3 Enter a number: 8 Enter a number: 0 Sum of the odd numbers you entered is...
Create a python script that reads in an unknown number of positive numbers and negative numbers....
Create a python script that reads in an unknown number of positive numbers and negative numbers. These numbers are to be stored in two different collections. Once the user enters a zero value, the script will output all of the values in both collections, separately. Sample Usage/Output: Please enter a positive or negative number (enter a zero to end): 5 Please enter a positive or negative number (enter a zero to end): -7 Please enter a positive or negative number...
(Write in C++) Write a program that reads in two numbers and, if the input is...
(Write in C++) Write a program that reads in two numbers and, if the input is valid, outputs 2 times the product of the integers that lie between the two values (including the values themselves). If either number is not an integer, or if the first number is not less than the second number, just output an error message. The sample runs below should give the idea. User inputs are in bold. Important Notes: Your program should use a loop...
C++ [Program-2] Write a program that accepts an indefinite set of numbers until the user enters...
C++ [Program-2] Write a program that accepts an indefinite set of numbers until the user enters “-1”. In other words, the program keeps accepting new values until the user provides a “-1” (your program accepts all values, and discards the “-1”). When done, the program prints back to the user: (i) the sum of all numbers entered (except -1), (ii) the minimum value seen across all numbers (except -1), and (iii) the maximum value across all numbers (except -1).
C program fractions.c that does the following: 1. The program starts by making the user enter...
C program fractions.c that does the following: 1. The program starts by making the user enter a non-negative integer number a, called the first numerator. If the user enters a negative number, the program repeats the entry. 2. The program then makes the user enter a positive integer number b, called the first denominator. If the user enters a non-positive number, the program repeats the entry. 3. The program then makes the user enter a non-negative integer number c, called...
1)Write a program that asks a user for a number. If (and only if) the number...
1)Write a program that asks a user for a number. If (and only if) the number is greater than zero print “The number is valid” 2)Write a program that asks a user for a grade. If (and only if) the grade is greater than zero and less than 101, print “The grade is valid” 3)Write a program that asks a user how many widgets they want to buy and prints out what the user should pay. Widgets costs 10 dollars....
IN C LANGUAGE This program initially reads two integers from standard input: (1) an integer T...
IN C LANGUAGE This program initially reads two integers from standard input: (1) an integer T and (2) a positive integer N. It then reads N integers and counts the numbers that are greater than T and the numbers than are less than T. It then prints out these two counts. Example What is the threshold value? 7 How many values? 5 Enter 5 values: 6 7 9 9 8 3 values are greater than 7 1 values are less...
C++ INSTRUCTIONS Problem Specification : Write a program that reads a group of numbers from the...
C++ INSTRUCTIONS Problem Specification : Write a program that reads a group of numbers from the user and places them in an array of type float. Once the numbers are stored in the array, the program should average and print the result. Use pointer notation wherever possible. Program Output (with Input Shown in Bold): Enter number: 2 Enter another (y/n)? y Enter number: 4 Enter another (y/n)? y Enter number: 6 Enter another (y/n)? y Enter number: 8 Enter another...
Write a program that asks the user to enter a series of numbers separated by commas....
Write a program that asks the user to enter a series of numbers separated by commas. Here is an example of valid input: 7,9,10,2,18,6 The program should calculate and print the sum of all the numbers. Sample Run java NumberSum Enter·numbers·separated·by·commas:1,2,3↵ 6↵
Write the following ANNA assembly language programs. 1.HighestnumberWrite an ANNA assembly program (high.ac) that will continuously...
Write the following ANNA assembly language programs. 1.HighestnumberWrite an ANNA assembly program (high.ac) that will continuously prompt the user for numbers. When the user enters a negative number, print the highest positive number entered by the user and exit the program. Print a zero if they did not enter any positive numbers. 2.DivisionWrite an ANNA assembly program (div.ac) that divides two positive numbers that come from user input and returns both the quotient and the remainder. For example, if the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT