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.
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;
}
=================================
Get Answers For Free
Most questions answered within 1 hours.