C++ Program to Sum Positive and Negative Numbers in Array
Write C++ Program to Sum Positive and Negative Numbers in Array
// CPP Program to Sum Positive and Negative Numbers in Array #include <iostream> using namespace std; int main() { int A[] = {1, 2, 3, -1, -2, -3}; int sum_positive = 0,sum_negative = 0,i,n; n = sizeof(A) / sizeof(A[0]); cout << "Array Elements :--> "; for(i=0; i<n; i++) cout << A[i] << " "; for(i = 0;i < n ;i++) { if(A[i] > 0) sum_positive += A[i]; else sum_negative += A[i]; } cout << "\n\nSum of Positive Numbers :--> " << sum_positive; cout << "\nSum of Negative Numbers :--> " << sum_negative; return 0; }
Output:
Array Elements :--> 1 2 3 -1 -2 -3
Sum of Positive Numbers :--> 6
Sum of Negative Numbers :--> -6