Advertisement
Spocoman

10. Odd Even Sum

Sep 6th, 2023
1,038
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.59 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     int num;
  7.     cin >> num;
  8.  
  9.     int number;
  10.  
  11.     int oddSum = 0;
  12.     int evenSum = 0;
  13.  
  14.     for (int i = 1; i <= num; i++) {
  15.         cin >> number;
  16.  
  17.         if (i % 2 == 1) {
  18.             oddSum += number;
  19.         }
  20.         else {
  21.             evenSum += number;
  22.         }
  23.     }
  24.  
  25.     if (oddSum == evenSum) {
  26.         cout << "Yes" << endl;
  27.         cout << "Sum = " << oddSum << endl;
  28.     }
  29.     else {
  30.         cout << "No" << endl;
  31.         cout << "Diff = " << abs(oddSum - evenSum) << endl;
  32.     }
  33.  
  34.     return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement