## C++ program to addition of even elements and subtraction of odd elements

In this blog, we will create a program to find the addition of even elements and subtraction of odd elements.

# What is an even or odd number?

Any integer ending in 0,2,4,6,8 and divisible by two with a remainder of zero is called an even number.

Example of even numbers: 34,-64,78,788,560

When any whole number ends in 0,1,3,5,7,9 and cannot be divided without a remainder, it is called an odd number.

Example for odd numbers: 33,-69,75,785

## Algorithm for the respective code

We can use the modular operator to find an odd or even number in an array

if n%2==0, n is an even number – if the number is even, the remainder is zero.

if n%2==1, n is an odd number – if the number is odd, the remainder is one.

### C++ code to find the addition of even elements and subtraction of odd elements

```#include <iostream>
using namespace std;

int main()
{
int arr[6]={50,45,40,35,30,25};
int i,oddSum=0,evenSum=0,final;

for(i=0; i<6; i++){
if(arr[i]%2==0){
evenSum=evenSum+arr[i];
}
else{
oddSum=arr[i]+oddSum;
}
}
cout<<"The sum of odd numbers are:"<<oddSum;
cout<<"\nThe sum of even numbers are:"<<evenSum;
final=evenSum-oddSum;
cout<<"\n final result which is:"<<final;
return 0;
}```

### Explanation of the above code

• First, define an array with elements.
• Next, declare and initialize two variables to find the sum as oddSum=0, evenSum=0
• Then use a “for loop” to remove the elements one by one from the array.
• The “if” statement finds the number, and if it’s an even number, adds it to the even sum. If the number is not even, it is resolved “else”.
• The “else” statement finds odd numbers and adds to the odd sum.
• Finally, the sums of the odd and even numbers are displayed.