C++ Program to output the sum of positive numbers

WAP to output the sum of positive numbers, sum of negative numbers, sum of even numbers,
sum of odd numbers from a list of numbers entered by the user. The list terminates when the
number entered is zero.

Showing Answers 1 - 1 of 1 Answers

JOYJIT DEB

  • Jul 19th, 2015
 

Code
  1. #include <iostream>

  2. using namespace std;

  3.  

  4. int main(){

  5.         signed int num;

  6.         static unsigned int l_SumOfPos = 0;

  7.         static signed int l_SumOfNeg = 0;

  8.         static unsigned int l_SumOfEven = 0;

  9.         static unsigned int l_SumOfOdd = 0;

  10.         while(1){

  11.                 cout << "Enter a number to be added to your list, 0 to exit" << endl;

  12.                 cin >> num;

  13.                 if(num != 0){

  14.                         if(num<0){

  15.                                 l_SumOfNeg = l_SumOfNeg + num;

  16.                         } else {

  17.                                 l_SumOfPos = l_SumOfPos + num;

  18.                                 if(num%2 == 0){

  19.                                         l_SumOfEven = l_SumOfEven + num;

  20.                                 } else {

  21.                                         l_SumOfOdd = l_SumOfOdd + num;

  22.                                 }

  23.                         }

  24.                 } else {

  25.                         break;

  26.                 }

  27.         }

  28.         cout << "Sum of All Positive numbers: " << l_SumOfPos << endl;

  29.         cout << "Sum of All Negative numbers: " << l_SumOfNeg << endl;

  30.         cout << "Sum of All Even numbers: " << l_SumOfEven << endl;

  31.         cout << "Sum of All Odd numbers: " << l_SumOfOdd << endl;

  32.         return 0;

  33. }

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions