# Calculate sum of odd and even in an array in C++

0 comment 651 views

In this tutorial, we will discuss how to use the  C++ program to calculate the sum of odd and even numbers in an array.

## What is an even or odd number?

When any integer ends in 0,2,4,6,8 and it can be divided by two with the remainder of zero, it is called as an even number.

Example of even numbers – 34,-64,78,788

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

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

Here, we can use a modular operator to find 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.

## Calculating the sum of odd and even numbers using “for loop” with pre-defined input.

This program allows the user to calculate the sum of odd and even numbers in the given array using “for loop”.

```.wp-block-code {
border: 0;
}

.wp-block-code > div {
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
```#include <iostream>
#include <conio.h>
using namespace std;

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

for(i=0; i<6; i++){
if(arr[i]%2==0){
evenSum=evenSum+arr[i];
}
else{
oddSum=oddSum+arr[i];
}
}
cout<<"The sum of odd numbers are:"<<oddSum;
cout<<"\nThe sum of even numbers are:"<<evenSum;
getch();
return 0;
}```Code language: PHP (php)```

Output

```The sum of odd numbers are: 105
The sum of even numbers are: 120```

## Calculating the sum of odd and even numbers using “for loop” taking input from user

This program allows the user to choose any value for array length and input values into the array.  The program will calculate the sum of odd and even numbers from the array using “for loop”.

``````#include <iostream>
#include <conio.h>
using namespace std;

int main()
{

int oddSum=0,evenSum=0;
int i,size;
cout<<"Enter the size of the array: ";
cin>>size;
int arr[size];
cout<<"Enter the Array elements: ";

for(i=0; i<size; i++){
cin>>arr[i];
}
for(i=0; i<size; i++){
if(arr[i]%2==0){
evenSum=evenSum+arr[i];
}
else{
oddSum=oddSum+arr[i];
}
}
cout<<"The sum of odd numbers are:"<<oddSum;
cout<<"\nThe sum of even numbers are:"<<evenSum;
getch();
return 0;
}```Code language: PHP (php)```

Output

```Enter the size of the array: 5
Enter the Array elements: 6 7
8
9
10
The sum of odd numbers are: 16
The sum of even numbers are: 24```

## Calculating the sum of odd and even numbers using “while loop” with pre-defined input.

This program allows the user to calculate the sum of odd and even numbers in the given array using “while loop”.

``````#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
int arr={45,40,35,30,25,20};
int i,oddSum=0,evenSum=0;

i=0;
while(i<6){
if(arr[i]%2==0){
evenSum=evenSum+arr[i];

}
else{
oddSum=oddSum+arr[i];
}
i++;
}
cout<<"The sum of odd numbers are:"<<oddSum;
cout<<"\nThe sum of even numbers are:"<<evenSum;
getch();
return 0;
}```Code language: PHP (php)```

Output

```The sum of odd numbers are: 105
The sum of even numbers are: 90```

## Calculating the sum of odd and even numbers using “while loop” taking input from user

This program allows the user to choose any value for array length and input values into the array.  The program is calculating the sum of odd and even numbers from the array using “while loop”.

``````#include <iostream>
#include <conio.h>
using namespace std;

int main()
{

int oddSum=0,evenSum=0;
int i,size;
cout<<"Enter the size of the array: ";
cin>>size;
int arr[size];
cout<<"Enter the Array elements: ";

i=0;
while(i<size){
cin>>arr[i];
i++;
}
i=0;
while(i<size){
if(arr[i]%2==0){
evenSum=evenSum+arr[i];
}
else{
oddSum=oddSum+arr[i];
}
i++;
}
cout<<"The sum of odd numbers are:"<<oddSum;
cout<<"\nThe sum of even numbers are:"<<evenSum;
getch();
return 0;
}```Code language: PHP (php)```

Output

```Enter the size of the array: 6
Enter the array elements: 15
24
33
12
24
35
The sum of odd numbers are:83
The sum of even numbers are:60```