120k views
3 votes
Write a program that reads an integer, and then prints the sum of the even and odd integers.

1 Answer

2 votes

Answer:

#include <iostream> // header file

using namespace std; // namespace

int main ()

{

int n, x1, i=0,odd_Sum = 0, even_Sum = 0; // variable declaration

cout << "Enter the number of terms you want: ";

cin >> n; // user input the terms

cout << "Enter your values:" << endl;

while (i<n) // iterating over the loop

{

cin >> x1; // input the value

if(x1 % 2 == 0) // check if number is even

{

even_Sum = even_Sum+x1; // calculate sum of even number

}

else

{

odd_Sum += x1; // calculate sum of odd number.

}

i++;

}

cout << "Sum of Even Numbers is: " << even_Sum << endl; // display the sum of even number

cout << "Sum of Odd Numbers is: " << odd_Sum << endl; // display the sum of odd number

return 0;

}

Output:

Enter the number of terms you want:3

Enter your values:2

1

78

Sum of Even Numbers is:80

Sum of Odd Numbers is:1

Step-by-step explanation:

In this program we enter the number of terms by user .After that iterating over the loop less then the number of terms .

Taking input from user in loop and check the condition of even. If condition is true then adding the sum of even number otherwise adding the sum of odd number.

Finally print the the sum of the even number and odd integers.