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
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...
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).
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...
Please use Python 3 4). Write a program that asks the user to enter 10 numbers....
Please use Python 3 4). Write a program that asks the user to enter 10 numbers. The program should store the numbers in a list and then display the following data: • The lowest number in the list • The highest number in the list •The total of the numbers in the list • The average of the numbers in the list   Sample input & output: (Prompt) Enter Number 1: (User enter) 4 (Prompt) Enter Number 2: (User enter) 7...
Write a program that does the following in order: 1. Ask user to enter a name...
Write a program that does the following in order: 1. Ask user to enter a name 2. Ask the user to enter five numbers “amount1”, “amount2”, “amount3”, “amount4”, “amount5” 3. Calculate the sum of the numbers “amount1”, “amount2”, “amount3”, “amount4”, “amount5” 4. If the sum is greater than 0, print out the sum 5. If the sum is equal to zero, print out “Your account balance is zero” 6. If the sum is less than 0, print out “Your account...
Write a C program that prompts the user to input as many unsigned(n) as the user...
Write a C program that prompts the user to input as many unsigned(n) as the user wants to type, terminated by non negative number You may assume without checking that when the user is supposed to type an unsigned, he nevertypes a double You may assume without checking that when the user is supposed to type an unsigned, he nevertypes a negative number. For each number the user types, output whether that number is prime or not. You won't need...
Written in MASM Assembly Problem Definition: Write a program to calculate Fibonacci numbers. • Display the...
Written in MASM Assembly Problem Definition: Write a program to calculate Fibonacci numbers. • Display the program title and programmer’s name. Then get the user’s name, and greet the user. • Prompt the user to enter the number of Fibonacci terms to be displayed. Advise the user to enter an integer in the range [1 .. 46]. • Get and validate the user input (n). • Calculate and display all of the Fibonacci numbers up to and including the nth...
MATLAB Task: Use a for loop to find how many numbers you have to add to...
MATLAB Task: Use a for loop to find how many numbers you have to add to get a sum greater than 1,000. The numbers in the problem are consecutive, so adding up 1+2+3+4+5+6+.... until you reach the user defined value 1000. Method : must Make a variable equal to 1 then add 2, 3, 4...to it, until you reach 1000, then you break out the loop once the sum hits 1,000.
Hi, I need a program in C, where it will prompt a user for a math...
Hi, I need a program in C, where it will prompt a user for a math quiz, either easy or medium. If the user choses difficulty easy, it will prompt the user for how many questions they would like from 1-5. If the user enters medium it will prompt the user on how many questions they would like from 1-10. After those prompts are entered questions will be displayed using rand(), the questions must be multiple choice or division, aswell...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT