Advertisement
Guest User

Sum even odd

a guest
Feb 26th, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. // CPP program to find out
  2. // Sum of elements at even and
  3. // odd index positions separately
  4. #include <iostream>
  5.  
  6.  
  7.  
  8. using namespace std;
  9.  
  10.  
  11. // Function to calculate sum
  12.  
  13. void EvenOddSum(int arr[], int n)
  14. {
  15.  
  16.     int even = 0;
  17.  
  18.     int odd = 0;
  19.  
  20.     for (int i = 0; i < n; i++) {
  21.  
  22.         // Loop to find even, odd sum
  23.  
  24.         if (i % 2 == 0)
  25.  
  26.             even += arr[i];
  27.  
  28.         else
  29.  
  30.             odd += arr[i];
  31.  
  32.     }
  33.  
  34.  
  35.  
  36.     cout << "Even index positions sum " << even;
  37.  
  38.     cout << "nOdd index positions sum " << odd;
  39. }
  40.  
  41.  
  42. // Driver function
  43.  
  44. int main()
  45. {
  46.  
  47.     int arr[] = { 1, 2, 3, 4, 5, 6 };
  48.  
  49.     int n = sizeof(arr) / sizeof(arr[0]);
  50.  
  51.  
  52.  
  53.     EvenOddSum(arr, n);
  54.  
  55.  
  56.  
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement